我按照步骤http://railscasts.com/episodes/197-nested-model-form-part-2
进行了操作我创建了一个包含供应商的选择框,因此当我选择供应商时,将显示所有选择供应商的采购,更新嵌套表单。
这是我的表格
suppliers
|id| |name|
1 AAAA
2 BBBB
shopping_documents
|id| |supplier_id|
1 1
2 2
shopping_products
|id| |shopping_document_id| |qty| |purchase_product_id|
1 1 1 1
2 1 1 2
3 2 1 3
purchase_products
|id| |name| |description| |cost| |supplier_id|
1 XP CD-ROM 1000 1
2 VISTA CD-ROM 2000 1
3 W7 CD-ROM 3000 2
这是控制器/app/controller/purchase_product_controller.rb
class ShoppingDocumentController < ApplicationController
def new
@document = ShoppingDocument.new
@suppliers= Supplier.all
@purchases = PurchaseProduct.all
1.times do
shopping_product = @document.shopping_products.build
end
end
def create
@document = ShoppingDocument.new(params[:shopping_document])
if @document.save
flash[:notice] = "Successfully created document."
redirect_to :action=>"index"
else
render :action => 'new'
end
end
def update_nested_div
@purchases= PurchaseProduct.find(:all,:conditions=>['supplier_id =? ',params[:suppliers] ])
end
end
这里的型号:
class ShoppingDocument < ActiveRecord::Base
has_many :shopping_products
accepts_nested_attributes_for :shopping_products ,:allow_destroy => true
end
class ShoppingProduct < ActiveRecord::Base
belongs_to :shopping_document
belongs_to :purchase_product
end
class PurchaseProduct < ActiveRecord::Base
has_many :shopping_products
end
以下是视图:/app/view/purchase_product/new.html.erb
<% form_for @document, :url => {:controller=>"shopping_document",:action=>'create'} do |f| %>
Name: <%= select_tag 'suppliers',"<option value=\"\">Select</option>"+options_for_select(@suppliers.collect {|t| [t.name,t.id]} ),:onchange=>remote_function(:url=>{:controller=>"shopping_document",:action=>"update_nested_div"},:with=>"'suppliers=' + $('suppliers').value"),:name=>"shopping_document[supplier_id]" %>
<% f.fields_for :shopping_products do |builder| %>
<%= render "shopping_product_fields", :f => builder %>
<% end %>
<p><%= link_to_add_fields "Add Product", f, :shopping_products %></p>
<p><%= f.submit "Submit" %></p>
<% end %>
以下是部分视图:/app/view/shopping_document/_shopping_product_fields.html.erb
<div class="nested_form">
<%= f.select :purchase_product_id,@purchases.map { |c| [c.cost, c.id] },{},:id=>"helping" %> %>
Quantity: <%= f.text_field :qty %> <%= link_to_remove_fields "remove", f %>
</div>
以下是rjs:/app/view/shopping_document/_update_nested_div.rjs
page.replace_html 'helping', options_for_select(@purchases.map{|c| [c.cost, c.id]})
工作正常,但只是使用第一个嵌套选择框,如果我添加点击“添加产品”,第二个或下一个或更多将不会根据供应商选择并显示所有供应商。
请有人帮我解决这个问题吗?
只是在第一个嵌套选择框上工作,如果我添加另一个,则根据所选择的供应商不显示值,只显示全部。
答案 0 :(得分:1)
编辑8/11 9pm
您可以在select标签上设置一个类而不是id,以根据供应商选择更改选项。
<%= f.select :purchase_product_id,@purchases.map { |c| [c.cost, c.id] }, {}, :class => "helping" %>
然后使用以下内容更改/app/view/shopping_document/_update_nested_div.rjs:
page.select('.helping').each do |select_tag|
page.replace_html select_tag, options_for_select(@purchases.map{|c| [c.cost, c.id]})
end
你遇到的问题是replace_html在它找到的第一个元素上使用id“help”。使用class和page.select,我们可以通过迭代选择标签的集合来获取选项。
我使用的模式在Rails 2.3.8 api documentation中描述。请查看其他可用方法。
答案 1 :(得分:0)
由于错误提及,f
未定义。使用select_tag
(http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-select_tag处的文档),如下所示:
<%= select_tag :purchase_product_id,@purchases.map { |c| [c.cost, c.id] } %> %>