根据http://www.treibstofff.de/2009/07/12/ruby-on-rails-23-nested-attributes-
with-ajax-support/找到了解决方法
这可能应该放在爆发助手中(在爆发控制器atm中)
def update_select_menus @outbreak_type = params[:outbreak_type].strip #next_child_index will only be used if @next_child_index ? params[:next_child_index] : 0 if params[:id] @outbreak = Outbreak.find(params[:id]) else @outbreak = Outbreak.new @outbreak.risks.build @outbreak.incidents.build @outbreak.locations.build end if @outbreak_type == "FOODBORNE" ob_type_query = "OUTBREAKS:TRANSMISSION_MODE:" << @outbreak_type @transmission_modes = Property.find(:all, :conditions => {:field => ob_type_query}) ob_type_query = "INVESTIGATIONS:CATEGORY:" << @outbreak_type @sample_types = Property.find(:all, :conditions => {:field => ob_type_query}) @categories = Category.find(:all, :conditions => { :outbreak_type => "FOODBORNE"}) @subcategories = Subcategory.find(:all, :conditions => { :category_id => @categories.first.id}) @subtypes = Subtype.find(:all, :conditions => { :subcategory_id => @subcategories.first.id}) elsif @outbreak_type == "NON-FOODBORNE" ob_type_query = "OUTBREAKS:TRANSMISSION_MODE:" << @outbreak_type @transmission_modes = Property.find(:all, :conditions => {:field => ob_type_query}) ob_type_query = "INVESTIGATIONS:CATEGORY:" << @outbreak_type @sample_types = Property.find(:all, :conditions => {:field => ob_type_query}) @categories = Category.find(:all, :conditions => { :outbreak_type => "NON-FOODBORNE"}) @subcategories = Subcategory.find(:all, :conditions => { :category_id => @categories.first.id}) @subtypes = Subtype.find(:all, :conditions => { :subcategory_id => @subcategories.first.id}) end @pathogen_types = Property.find(:all, :conditions => {:field => "PATHOGENS:CATEGORY"}) @outbreak_types = Property.find(:all, :conditions => {:field => "OUTBREAKS:OUTBREAK_TYPE"} ) render :update do |page| page.replace 'outbreak_transmission_div', :partial => 'transmission_mode_select_update' page.replace 'incident_category_select', :partial => 'incident_category_select_update' page.replace 'incident_subcategory_select', :partial => 'incident_subcategory_select_update' page.replace 'incident_subtype_select', :partial => 'incident_subtype_select_update' end end在“爆发”视图中(尽管由于此部分与“事件”相关,所以它可能应该在该视图中显示)
<% ActionView::Helpers::FormBuilder.new(:outbreak, @outbreak, @template, {}, proc{}).fields_for :incidents,{:child_index => @next_child_index} do |this_form| %><div id="incident_category_select"><%= render :partial => 'category_select', :locals => {:incident_form => this_form } %></div><% end %>ActionView :: Helpers :: FormBuilder用于生成所需的fields_for表单-网站文章对此进行了详细介绍。
结果索引由@next_child_index变量设置,该变量可以通过原始AJAX调用传递给控制器(例如@next_child_index =
1,那么结果表单元素名称将是 爆发[incidents_attributes] [1] [category_id]
)-我没有在此示例中使用此方法,因为尽管将来我希望该表单在每个爆发中都支持多个位置以进行此初始遍历,但它只接受一个位置-每个爆发均发生一个事件。
_category_select.erb部分(在“爆发”视图atm中)
<% with_str = "'category_id=' + value " %><% if @outbreak.id %><% with_str << "+ '&id=' + #{@outbreak.id}" %><% end %><%= incident_form.collection_select( :category_id, @categories, :id, :name, {:prompt => "Select a category"}, {:onchange => "#{remote_function(:url => { :action => "update_subcategory"}, :with => with_str)}"}) %>with_str只是有选择地将爆发ID传递给控制器(如果存在)以找到爆发记录以生成表单,如果不存在则创建一个新的爆发以及关联的嵌套属性(如事件和位置)。
必须采用更整洁的方式进行操作-尤其是FormHelper并通过可选的with字符串传递爆发ID。



