Ruby on rails 在RubyonRails中从1个ajax调用中获取2个变量
我想知道如何使用ajax调用获得2个变量来更新div标记,而不仅仅是1个。我当前的.js文件:Ruby on rails 在RubyonRails中从1个ajax调用中获取2个变量,ruby-on-rails,json,jquery,Ruby On Rails,Json,Jquery,我想知道如何使用ajax调用获得2个变量来更新div标记,而不仅仅是1个。我当前的.js文件: $(document).ready(function() { $("select").change(function() { var selected_product_id; selected_product_id = $(this).val(); $.ajax({ url: "/products/" + sele
$(document).ready(function() {
$("select").change(function() {
var selected_product_id;
selected_product_id = $(this).val();
$.ajax({
url: "/products/" + selected_product_id,
type: "GET",
success: function(data) {
$("#description").empty().append(data);
}
});
});
});
show.html.erb获取数据的位置:
<%= @product.item %>
与
show.html.erb:
<%= @product.item %>
<%= @product.price %>
啊,这让我想起了一周前我为你写的一段代码 您可以使用JSON呈现您的产品:
#controller
def show
@product = Product.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render :json => @product.to_json }
end
end
并按如下方式处理来自服务器的响应:
$.ajax({
url: "/products/" + selected_product_id,
dataType: 'JSON',
success: function(data) {
var product = JSON.parse(data);
$("#description").empty().append(product.item);
$("#price").empty().append(product.price);
}
});
编辑#1:我找到了这个方法:
jQuery.getJSON
:你可以使用JSON,返回
{"item": "<%=j @product.item %>", "price": <%= @produce.price %>}
{“项目”:““价格”:}
然后在ajax调用中
键入:“json”
,您是否考虑过使用form\u for
或form\u标签
?@derek\u duncan我在form\u for标签中有我的select标签和一些文本输入字段。如何使用它动态填充div?好的,谢谢。我应该在show.html.erb文件中放置什么?我尝试返回一个数组,如@user1737909所建议的,但没有成功。您可以保持show.html.erb的原样。如果您使用HTML格式向/products/[:id]
发出请求,则会呈现此文件。goll darnit仍然不起作用。对不起,我是个笨蛋。我已经用更新的文件编辑了我的问题。好的,当我导航到products/1.json时,它会输出以下内容:{“created_at”:null,“group”:“dairy”,“id”:1,“item”:“milk”,“price”:“3.0”,“updated_at”:null}。这似乎是正确的,所以我认为附加product.item product.price会起作用,但事实并非如此。你认为我需要安装一些东西才能让json正常工作吗?我有最新版本的ruby和rails,并且非常确定json是自动包含的。好吧,我终于让它开始工作了。我没有使用$.ajax,而是使用$.getJSON并按照您提供的链接上的说明进行操作。再次感谢您的帮助。我应该将该数组存储在变量中,然后使用或返回它吗?或者我需要在上面使用json.encode吗?如果你使用type:'json'
jquery将解析它的shuckydarn,我似乎无法让它工作。如果你想看的话,我已经用更新的文件编辑了我的问题。我尝试使用你的代码,但无法使其工作。
class ProductsController < ApplicationController
respond_to do |format|
format.html # show.html.erb
format.json { render :json => @product.to_json }
end
def index
end
def show
@product = Product.find(params[:id])
end
end
$(document).ready(function() {
$("select").change(function() {
var selected_product_id;
selected_product_id = $(this).val();
$.getJSON("/products/"+selected_product_id,function(data){
$("#description").empty().append(data.item);
$("#price").empty().append(data.price);
});
});
});
#controller
def show
@product = Product.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render :json => @product.to_json }
end
end
$.ajax({
url: "/products/" + selected_product_id,
dataType: 'JSON',
success: function(data) {
var product = JSON.parse(data);
$("#description").empty().append(product.item);
$("#price").empty().append(product.price);
}
});
{"item": "<%=j @product.item %>", "price": <%= @produce.price %>}