0

我的 Item 模型有一个表单,它的 :make 属性。根据选择的选项,它应该呈现共享相同 :item_make 值的相应产品。因此,如果您在第一个表单上选择“Hoodies”,则可用的不同连帽运动衫将显示在第二个表单上。任何帮助将不胜感激。

引用

attr_accessible :items_attributes
has_many :items

产品

attr_accessible :name, :item_make
has_many :items

物品

attr_accessible :make, :product_id
belongs_to :quote
belongs_to :product

两个输入:(在嵌套报价表单内 -> 项目表单)

<%= f.input :make, collection: clothing_types, label: 'Thread Type', remote: true, 
url: url_for(controller: 'quotes', action: 'update_products'), 
type: 'json', input_html: {class: "select_make"} %> 

<%= f.input :product_id, input_html: {class: "select_product"} %>

目前@products 是所有产品的列表。

引号_控制器:

def new
  @quote = Quote.new
  @quote.items.build
  @item = Item.new
  @products = Product.all
end

def update_products
  @make = params[:make]
  @subproducts = Product.where(:item_make => @make).all
  render json: @subproducts.map{|x| [x.id, x.name]}
end

**编辑:**Application.js

$(document).on('change', '.select_make', function() {

    $.ajax({
        type: 'GET',
        url: "/quotes/update_products",
        success: function(data) {
        if (data.content == 'None') 
            {
            $('.select_product').empty();
            $('.select_product').append( $('<option>No make provided!</option>'));
            }
        else
            $('.select_product').empty();
            $('.selecT_product').append(); //unfinished
        }
    });
});

我已经尝试在Ruby on Rails 上使用 jquery 的不同变体 - 更改事件的下拉框,但还不能让它工作。

如果有人能告诉我执行此操作所需的 javascript,那就太棒了

4

1 回答 1

0

做这样的事情怎么样:

def update_products
  @make = params[:make]
  @subproducts = Product.where(:item_make => @make).all
  respond_with json: @subproducts.map{|x| [x.id, x.name]}
end


$.ajax({
    type: 'GET',
    url: "/quotes/update_products.json",
    // Rest of the code....
});
于 2013-10-14T21:15:40.347 回答