【问题标题】:Getting 2 variables from 1 ajax call in ruby on rails从 ruby​​ on rails 的 1 个 ajax 调用中获取 2 个变量
【发布时间】:2013-06-06 07:30:21
【问题描述】:

我想知道如何使用 ajax 调用获取 2 个变量来更新 div 标签,而不仅仅是 1 个。我当前的 .js 文件:

    $(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 %>

我想要这样的东西

        $.ajax({
          url: "/products/" + selected_product_id,
          type: "GET",
          success: function(data) {
          $("#description").empty().append(data[1]);
          $("#price").empty().append(data[2]);
          }
        });

        <%= @product.item %>
        <%= @product.price %>

我的描述 div 用@product.item 更新,我的价格 div 用@product.price 更新。我怎样才能做到这一点?

编辑

更新的 .js 文件

    $(document).ready(function() {
      $("select").change(function() {
        var selected_product_id;
        selected_product_id = $(this).val();
        $.ajax({
          url: "/products/" + selected_product_id,
          type: "json",
          success: function(data) {
          $("#description").empty().append(product.item);
          $("#price").empty().append(product.price)
          }
        });
      });
    });

show.html.erb:

    <%= @product.item %>
    <%= @product.price %>

控制器:

    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

我很确定控制器不正确。我将@product 放在respond_to 之前,但它没有用,所以我只是把respond_to 放在了不知道它去哪里的情况下。很抱歉成为这样的菜鸟。感谢您的所有帮助。

最终编辑:

工作 javascript 文件:

    $(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);
        });
      });
    });

【问题讨论】:

  • 您是否考虑过使用form_forform_tag
  • @derek_duncan 我在 form_for 标签中有我的选择标签和一些文本输入字段。我如何使用它来动态填充 div?

标签: ruby-on-rails json jquery


【解决方案1】:

啊,这让我想起了我一周前为你写的一段代码!

您可以使用 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.getJSONhttp://api.jquery.com/jQuery.getJSON/

【讨论】:

  • 好的,谢谢。我应该在我的 show.html.erb 文件中放什么?我尝试返回一个类似@user1737909 建议的数组,但没有成功。
  • 您可以将 show.html.erb 保持原样。如果您使用 HTML 格式向/products/[:id] 发出请求,该文件将被呈现。
  • goll darnit 它仍然无法正常工作。很抱歉成为这样的菜鸟。我已经用更新的文件编辑了我的问题。
  • 好吧,当我导航到 products/1.json 它输出这个:{"created_at":null,"group":"dairy","id":1,"item":"milk" ,"价格":"3.0","updated_at":null}。这似乎是正确的,所以我认为附加 product.item product.price 会起作用,但事实并非如此。你认为我需要安装一些东西才能让 json 工作吗?我有最新版本的 ruby​​ 和 rails,我很确定 json 会自动包含在内。
  • 好吧,我终于让它工作了。我没有使用 $.ajax,而是使用了 $.getJSON 并按照您提供的链接上的说明进行操作。再次感谢您的帮助。
【解决方案2】:

你可以使用 JSON,返回

{"item": "<%=j @product.item %>", "price": <%= @produce.price %>}

然后在ajax调用中type: 'json'

【讨论】:

  • 我应该将该数组存储在一个变量中,然后使用 或 返回它吗?还是我需要在上面使用 json.encode?​​span>
  • 如果你使用type: 'json'jquery会解析它
  • shuckydarn 我似乎无法让它工作。如果您想看一下,我已经用更新的文件编辑了我的问题。我尝试使用您的代码,但无法正常工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-09
相关资源
最近更新 更多