jQuery Rails 4:将 JSON 对象(来自 AJAX 响应)渲染到视图中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23688049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails 4: Rendering JSON object (from AJAX response) into view
提问by Giuseppe
I'm using Rails 4 and I'm quite new to JSON objects.
我正在使用 Rails 4,我对 JSON 对象很陌生。
I have a controller:
我有一个控制器:
class UsersController < ApplicationController
def select_users
@users = User.all
respond_to do |format|
format.html
format.js {}
format.json {
render json: {:users => @users}
}
end
end
end
And a javascript users.js:
还有一个 javascript users.js:
$.ajax({
url: "/users/select_users",
dataType: "json",
data: {
prezzario: ui.item.value
},
success: function(data) {
$('#usersList').append(data.users);
}
});
Now I want to render this collection of users in a div, but I don't know how. In the data response I have in the console the correct data, with an object like this:
现在我想在一个 div 中呈现这个用户集合,但我不知道如何。在数据响应中,我在控制台中有正确的数据,对象是这样的:
{"users":[{"_id":{"$oid":"536cf7b6b8dd181f4b7acf9f"},"children":[{"$oid":"536cf7b6b8dd181f4b7acfa0"},{"$oid":"536cf7b7b8dd181f4b7acfd1"}],"description":"Some description.","name":"My name","user_id":"1"},{...........}]}
I have a partial /users/_users.html.erb with classic users.each and prints of all properties. If I try
我有一个部分 /users/_users.html.erb 与经典的 users.each 和所有属性的打印。如果我尝试
$('#usersList').append("<%= render 'users', :locals{ :users => "+data.users+"} %>");
I find in the div #usersList <%= render 'users', :locals{ :users => undefined }%>
我在 div 中找到 #usersList <%= render 'users', :locals{ :users => undefined }%>
I want to create a partial template and use it to render users. How can I do this?
我想创建一个部分模板并使用它来呈现用户。我怎样才能做到这一点?
回答by negative
There are 3 solutions (that i can think of):
有3个解决方案(我能想到的):
Your controller can render HTML on the backend that you append to your js, like
class UsersController < ApplicationController def select_users @users = User.all render "view", :users => @users end
and
success: function(data) { $('#usersList').append(data); }
You can use something like Moustache or Handlebars. Never used it, so I can't do a proper example. Sorry.
You can render the html in side the success code like
success: function(data){ data.users.forEach(function(user){ $user_html = $("<div class='user'>" + user.name + "<!-- etc --!/></div>"); $('#usersList').append($user_html); }); });
您的控制器可以在您附加到 js 的后端呈现 HTML,例如
class UsersController < ApplicationController def select_users @users = User.all render "view", :users => @users end
和
success: function(data) { $('#usersList').append(data); }
您可以使用 Mustache 或 Handlebars 之类的东西。从来没有用过,所以我不能做一个正确的例子。对不起。
您可以在成功代码旁边呈现 html,例如
success: function(data){ data.users.forEach(function(user){ $user_html = $("<div class='user'>" + user.name + "<!-- etc --!/></div>"); $('#usersList').append($user_html); }); });
回答by Mantosh
You have to $.parseJSON(data);
first.
你必须$.parseJSON(data);
先。
Like
喜欢
$.ajax({
url: "/users/select_users",
dataType: "json",
data: {
prezzario: ui.item.value
},
success: function(data) {
var r = $.parseJSON(data);
console.log(r.users)//This is where Your users are
}
});