Topic: Creating new model attributes on existing models

I am extending my application further. In the primary stage I had a Batch that had many Samples, I developed it using the complex forms from rails cast and was successful in achieving the goal. Now I am continuing the project further by nesting Experiment model to the Sample model.

I use the nested attributes again but my goal is different. I have provided the user with a page displaying all the samples that were added by him/her in the first place that allows him to select specific samples for adding some further sample information.

So, here the Sample details are getting updated. But I would also like to allow the user to fill in Experiment details for those selected Samples in one step. Hence, the sample details would be updated with creation of new experiment attributes.

The associations are

Samples has_many experiments
Experiment belongs_to samples.

I have used Nested resources as well

map.resoruces :samples do |sample|
sample.resources :experiments
end

and my View looks like this

<% form_tag create_sample_details_samples_path, :method => :put do %>
    <% for sample in @samples %>
        <% fields_for "samples[]", sample do |f| -%>
            <fieldset>
                <legend>Sample Name: <%= sample.name %></legend>
                <p><center><%= f.label :sample_title %>
                <%= f.text_field :sample_title, :size => 25 %></center></p>
                <table>
                    <tr>
                        <td>
                            <%= f.label :taxon_id %>
                            <%= f.text_field :taxon_id, :size => 15 %>
                        </td>
                        <td>
                            <%= f.label :scientific_name %>
                            <%= f.text_field :scientific_name, :size => 20 %>
                        </td>
                        <td>
                            <%= f.label :common_name %>
                            <%= f.text_field :common_name, :size => 15 %>
                        </td>
                    </tr>
                </table>
                <p>
                    <center>
                        <%= f.label :sample_descripition %><br \>
                        <%= f.text_area :description %>
                    </center>
                </p>
            </fieldset>

            <fieldset>
                <legend>Experiment Details</legend>
                    <% f.fields_for :expts do |f_expt| %>
                    <table>
                        <tr>
                            <td><%= f_expt.label :experiment_title  %>
                                <%= f_expt.text_field :expt_title, :size => 15 %></td>
                            <td><%= f_expt.label :design_description %><br />
                                <%= f_expt.text_area :design_descrp %></td>
                        </tr>
                        <tr>
                            <td>
                                <%= f_expt.label :library_name %>
                                <%= f_expt.text_field :lib_name, :size => 15 %>
                            </td>
                            <td>
                                <%= f_expt.label :library_strategy %>
                                <%= f_expt.select :lib_strategy, [['WGS ', 'WGS'], ['WXS ', 'WXS'], ['RNA-Seq ', 'RNA-Seq'], ['WCS ', 'WCS'], ['CLONE ', 'CLONE'], ['OTHER', 'OTHER'], ['Unspecified', 'unspecified']] %></td>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <%= f_expt.label :library_source %>
                                <%= f_expt.select :lib_source, [['GENOMIC', 'GENOMIC'], ['TRANSCRIPTOMIC', 'TRANSCRIPTOMIC'], ['METAGENOMIC', 'METAGENOMIC'], ['METATRANSCRIPTOMIC', 'METATRANSCRIPTOMIC'], ['SYNTHETIC (Synthetic DNA)', 'SYNTHETIC'], ['VIRAL RNA (Viral RNA)', 'VIRAL RNA'], ['OTHER', 'unspecified']] %></td>
                            </td>
                            <td>
                                <%= f_expt.label :library_selection %>                               
                                <%= f_expt.select :lib_selection, [['RANDOM', 'RANDOM'], ['PCR', 'PCR'], ['RANDOM PCR', 'RANDOM PCR'], ['other', 'other'], ['unspecified', 'unspecified']], :selected => 'unspecified' %>
                            </td>
                        </tr>
                    </table>
                    <center><%= f_expt.label :library_construction_protocol %><br \>
                            <%= f_expt.text_area :lib_construction %>
                    </center>
                    <% end %>                   
            </fieldset>   
            <!--  End  -->
           
           
        <% end %>
    <% end %>
   
    <p><center><%= submit_tag "Next" %></center></p>

<% end %>

I use form_tag so that I could use the selected sample_ids that were selected and also use f.fields_for :expts for the experiment values to be incorporated.

but the error I get is

Couldn't find Sample with ID=expts_attributes




The Controller action has the following code

  def add_sample_details
    @samples = Sample.find(params[:sample_ids])
    @samples.each do |sample|
      sample.expts.build
    end
  end

def create_sample_details
    @samples = Sample.update(params[:samples].keys, params[:samples].values).reject { |p| p.errors.empty? }
     if @samples.empty?
        flash[:notice] = "Sample details taken"
        redirect_to edit_sample_expts
      else
        render :action => "add_sample_details"
      end
  end


All suggestions are welcome

Cheers

Last edited by a1aks (2012-04-23 09:25:59)