1

我目前的工作环境是Rails 2.3.8(为什么我的公司没有搬到Rails 3的各种原因)。 我试图通过AJAX调用来更新多模型表单的元素 - 这个想法是要取决于用户如何选择或填充其他字段的某些下拉菜单。AJAX update_knownted_attributes_for partials

我以前设法通过使用基于非形式的partials来实现这个工作 - 现在的问题是重现当partials基于form_for和fields_for时,选择下拉列表的AJAX更新。

对不起,以下墙壁文字 - 我试图尽可能减少它(代码本身在我的测试网站上工作)。

如何在爆发控制器中生成表单构建器元素,然后将其传递给部分类别以取代incident_form?

任何指针将是巨大的:d

模型

class Outbreak < ActiveRecord::Base 
     has_many :incidents, :dependent => :destroy 
     has_many :locations, :through => :incidents 

    accepts_nested_attributes_for :locations, :allow_destroy => true, :reject_if => :all_blank 
    accepts_nested_attributes_for :incidents, :allow_destroy => true, :reject_if => :all_blank 
end 

class Incident < ActiveRecord::Base 
    belongs_to :outbreak 
    belongs_to :location 
    belongs_to :category 
    belongs_to :subcategory 
    belongs_to :subtype 

end 

class Location < ActiveRecord::Base 
    has_many :incidents, :dependent => :destroy 
    has_many :outbreaks, :thorugh => incidents 
end 

查看

_form

<% form_for(@outbreak, :html => {:multipart => true}) do |form| %> 

    <%= render :partial => 'outbreak_type_select', :locals => {:outbreak_types => @outbreak_types, :f => form } %> 
    <% form.fields_for :incidents do |incident_form| %> 
     <%= render :partial => 'category_select', :locals => {:categories => @categories, :incident_form => incident_form} %> 
     <%= render :partial => 'subcategory_select', :locals => { :subcategories => @subcategories, :incident_form => incident_form } %> 

    <% end %> 
<% end %> 

_outbreak_type_select

<% with_str = "'outbreak_type=' + value " %> 
<% if @outbreak.id %> 
<% with_str << "+ '&id=' + #{outbreak.id}" %> 
<% end %> 
<%= f.collection_select(:outbreak_type, @outbreak_types, :property_value, :property_value, {}, {:onchange => "#{remote_function(:url => { :action => "update_select_menus"}, :with => with_str)}"} ) %> 

_category_select

呼吁update_select_menus如何产生incident_form

<%= incident_form.collection_select(:category_id, @categories, :id, :name, {:prompt => "Select a category"}, {:onchange => "#{remote_function(:url => { :action => "update_subcategory"}, :with => "'category_id='+value")}"}) %> 

RJS

begin 
    page.replace_html 'outbreak_transmission_div', :partial => 'outbreaks/transmission_mode_select', :locals => {:transmission_modes => @transmission_modes } 
    rescue 
    page.insert_html :bottom, 'ajax_error', '<p>Error :: transmission modes update select</p>' 
    page.show 'ajax_error' 
    end 
    begin 
    page.replace_html 'incident_category_select', :partial => 'outbreaks/category_select', :locals => { :categories => @categories } 
    rescue 
    page.insert_html :bottom, 'ajax_error', '<p>Error :: incident category update select</p>' 
    page.show 'ajax_error' 
    end 

控制器

爆发后

def new 
     @outbreak = Outbreak.new 

     @outbreak.incidents.build 
     @outbreak.locations.build 

     #just the contents for the dropdowns 
     @categories = Category.find(:all, :conditions => {:outbreak_type => "FOODBORNE"}, :order => "outbreak_type ASC") 
     @subcategories = Subcategory.find(:all, :order => "category_id ASC") 

    end 

    def update_select_menus 
     @outbreak_type = params[:outbreak_type].strip 
     if params[:id] 
     @outbreak = Outbreak.find(params[:id]) 
     else 
     @outbreak = Outbreak.new 
     @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 

    respond_to do |format| 
      format.html 
      format.js 
     end 

    end 

回答

1

附近发现基于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表单 - 网站文章更详细地介绍了这一点。

所得指数通过其可以由原始AJAX调用被传递给控制器​​(例如@next_child_index = 1 @next_child_index变量设置,然后将得到的形式的元素名称将是爆发[incidents_attributes] [1] [category_id]) - 我没有在这个例子中使用过它,因为尽管将来我希望表单支持每个爆发对于这个初始运行的多个位置,但它只会接受每个爆发的单个位置 - 事件。

_category_select.erb部分(在爆发视图大气压)

<% 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,并通过可选字符串传递爆发ID。

+0

你从哪里得到@template变量?即时尝试实现这一点,但我有一个与模板的问题[请参阅我的帖子了解更多信息](http://stackoverflow.com/questions/7272814/passing-actionviewhelpersformbuilder-to-a-partial) – jalagrange 2011-09-01 15:43:11

+0

想'@模板'是由Rails 2.3应用程序生成的默认变量。如果您使用的是Rails 3>,那么@template将是零,你可能必须看看view_context http://stackoverflow.com/questions/3300582/rails-3-template-variable-inside-controllers-is-零 – Pasted 2011-09-05 10:06:47