Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Found a work around based on <a href="http://www.treibstofff.de/2009/07/12/ruby-on-rails-23-nested-attributes-with-ajax-support/" rel="nofollow">http://www.treibstofff.de/2009/07/12/ruby-on-rails-23-nested-attributes-with-ajax-support/</a></p> <p>This should probably go in Outbreak helper (in Outbreak controller atm)</p> <pre><code> 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:" &lt;&lt; @outbreak_type @transmission_modes = Property.find(:all, :conditions =&gt; {:field =&gt; ob_type_query}) ob_type_query = "INVESTIGATIONS:CATEGORY:" &lt;&lt; @outbreak_type @sample_types = Property.find(:all, :conditions =&gt; {:field =&gt; ob_type_query}) @categories = Category.find(:all, :conditions =&gt; { :outbreak_type =&gt; "FOODBORNE"}) @subcategories = Subcategory.find(:all, :conditions =&gt; { :category_id =&gt; @categories.first.id}) @subtypes = Subtype.find(:all, :conditions =&gt; { :subcategory_id =&gt; @subcategories.first.id}) elsif @outbreak_type == "NON-FOODBORNE" ob_type_query = "OUTBREAKS:TRANSMISSION_MODE:" &lt;&lt; @outbreak_type @transmission_modes = Property.find(:all, :conditions =&gt; {:field =&gt; ob_type_query}) ob_type_query = "INVESTIGATIONS:CATEGORY:" &lt;&lt; @outbreak_type @sample_types = Property.find(:all, :conditions =&gt; {:field =&gt; ob_type_query}) @categories = Category.find(:all, :conditions =&gt; { :outbreak_type =&gt; "NON-FOODBORNE"}) @subcategories = Subcategory.find(:all, :conditions =&gt; { :category_id =&gt; @categories.first.id}) @subtypes = Subtype.find(:all, :conditions =&gt; { :subcategory_id =&gt; @subcategories.first.id}) end @pathogen_types = Property.find(:all, :conditions =&gt; {:field =&gt; "PATHOGENS:CATEGORY"}) @outbreak_types = Property.find(:all, :conditions =&gt; {:field =&gt; "OUTBREAKS:OUTBREAK_TYPE"} ) render :update do |page| page.replace 'outbreak_transmission_div', :partial =&gt; 'transmission_mode_select_update' page.replace 'incident_category_select', :partial =&gt; 'incident_category_select_update' page.replace 'incident_subcategory_select', :partial =&gt; 'incident_subcategory_select_update' page.replace 'incident_subtype_select', :partial =&gt; 'incident_subtype_select_update' end end </code></pre> <p>In the Outbreak view (although since this partial is related to Incident it should probably go in that view instead)</p> <pre><code> &lt;% ActionView::Helpers::FormBuilder.new(:outbreak, @outbreak, @template, {}, proc{}).fields_for :incidents,{:child_index =&gt; @next_child_index} do |this_form| %&gt; &lt;div id="incident_category_select"&gt; &lt;%= render :partial =&gt; 'category_select', :locals =&gt; {:incident_form =&gt; this_form } %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p>The ActionView::Helpers::FormBuilder is used to produce the required fields_for form - The website article goes through this in more detail.</p> <p>The resulting index is set by the @next_child_index variable which can be passed to the controller by the original AJAX call (for example @next_child_index = 1, then the resulting form element name will be <strong>outbreak [incidents_attributes] [1] [category_id]</strong> ) - I haven't used this in this example because although in future I want the form to support more than one location per Outbreak for this initial run-through it will just accept a single Location - Incident per Outbreak.</p> <p>_category_select.erb partial (in Outbreak view atm)</p> <pre><code>&lt;% with_str = "'category_id=' + value " %&gt; &lt;% if @outbreak.id %&gt; &lt;% with_str &lt;&lt; "+ '&amp;id=' + #{@outbreak.id}" %&gt; &lt;% end %&gt; &lt;%= incident_form.collection_select( :category_id, @categories, :id, :name, {:prompt =&gt; "Select a category"}, {:onchange =&gt; "#{remote_function(:url =&gt; { :action =&gt; "update_subcategory"}, :with =&gt; with_str)}"}) %&gt; </code></pre> <p>The with_str is just to optionally pass the Outbreak id if it exists to the controller to find the Outbreak record to produce the form and if not to build a new Outbreak and associated nested attributes like Incidents and Locations.</p> <p>The must be neater ways of doing this - especially the FormHelper and passing the Outbreak id via the optional with string.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload