Re: Multiple rows of data with a single form

Hi,

After trying ankit644's code. I found this error message displaying regularly and I feel I have no values some where which makes it nil.

the errors shown are :

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each

Can any body tell me what mistake I am creating...

my view looks some think like this

<h2>New sample</h2>
<% form_tag :action => 'create' do %>
<% @samples.each_with_index do |sample, index| %>
<% fields_for "samples[#{index}]", sample do |f| %>
<table>
    <tr>
         <%= f.hidden_field :batch_id %>
        <th><%= f.label :sample_name %><br />
        <%= f.text_field :sname %></th>
        
        <th><%= f.label :species %><br />
        <%= f.text_field :organism, :rows => 2 %></th>
        
        <th><%= f.label :sample_type %><br />
        <%= f.select(:samp_type, %w{ DNA RNA}) %></th>
      
         <th><%= f.label :initial_concentration %><br />
        <%= f.text_field :ini_conc %></th>
      
         <th><%= f.label :initial_volume %><br />
        <%= f.text_field :ini_vol %></th>
       
        <th>
        <%= f.label :storage_space %><br />
        <%= f.text_field :storage_space %></th>
   
        <th>
        <%= f.label :sample_comment %><br />
        <%= f.text_area :samp_comment, :rows => 1 %></th>
      </tr>   
</table>
    <% end %>
    <% end %>
  <p><%= f.submit 'Submit' %></p>
<% end %>

Last edited by a1aks (2011-06-21 10:15:26)

Re: Multiple rows of data with a single form

The new action for the samples controller needs to set up the @samples variable
To cater for the scenario where there may be no samples wrap that .each up in a condition that checks for empty something like this

<% @samples.each_with_index do |sample, index| unless @samples.empty?%>
What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: Multiple rows of data with a single form

jamesw wrote:

The new action for the samples controller needs to set up the @samples variable
To cater for the scenario where there may be no samples wrap that .each up in a condition that checks for empty something like this

<% @samples.each_with_index do |sample, index| unless @samples.empty?%>

Re: Multiple rows of data with a single form

hey jamesw,

I tried the way you suggested me to get to the answer but I have been still unsuccessful.
My main reason is I have made two scaffolds for batches and models separately and i have made a partial for the sample.new.html in the batches folder. also I would like to know which controller should I edit, meaning as I have two controllers now, batch-controller as well as sample-controller, the methods of new and create should come in which bit...

Thank you but I am unfortunate in solving this problem

Re: Multiple rows of data with a single form

chuck everything people... I think I have the solution ...

Follow this link and you will see it

Thanks to Stephen...
http://www.stephenchu.com/2008/03/param … index.html

Re: Multiple rows of data with a single form

Hi...

Back again..

I am facing a problem again..

After I submit details in the form, i get an error message 
undefined method `stringify_keys!

Parameters:

{"commit"=>"Submit",
"authenticity_token"=>"b7fc41meCHEXhi7CEpEp/dlZou4PL2D+7J/54YEmlLA=",
"batch"=>{"submit_date(2i)"=>"6",
"bname"=>"",
"submit_date(3i)"=>"22",
"sample_no"=>"",
"submit_date(4i)"=>"14",
"grp_leader"=>"",
"submit_date(5i)"=>"08",
"contact_per"=>"",
"batch_comment"=>"",
"submit_date(1i)"=>"2011"},
"samples"=>{"1"=>{"organism"=>"",
"ini_vol"=>"",
"ini_conc"=>"",
"sname"=>""},
"2"=>{"organism"=>"",
"ini_vol"=>"",
"ini_conc"=>"",
"sname"=>""},
"samp_type"=>"gDNA",
"3"=>{"organism"=>"",
"ini_vol"=>"",
"ini_conc"=>"",
"sname"=>""}}}



looking at the last bit where sname is given, it shows that I have a hash value that takes all the parameters as strings
but not the one when i have selection tags, or text areas. could any one help me with this..

my codes are

Batches-controller
  def create
    @batch = Batch.new(params[:batch])   
   @samples = @batch.samples.build params[:samples].values
    if @batch.save
      flash[:success] = 'Good.'
      redirect_to :action => 'index'
    else
      flash[:error] ='Bad.'
       render :action => 'new'
    end
  end


## view-batches

<h1>New batch Details</h1>

<% form_for(@batch) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :Batch_name %><br />
    <%= f.text_field :bname %>
  </p>
  <p>
    <%= f.label :submit_date %><br />
    <%= f.datetime_select :submit_date %>
  </p>

    <p>
        <%= f.label :number_of_samples %><br />
        <%= f.text_field :sample_no %>
      </p>
   
  <p>
    <%= f.label :group_leader %><br />
    <%= f.text_field :grp_leader %>
  </p>
  <p>
    <%= f.label :contact_per %><br />
    <%= f.text_field :contact_per %>
  </p>
  <p>
    <%= f.label :batch_comment %><br />
    <%= f.text_area :batch_comment, :rows => 3 %>
  </p>

    <%= render :partial => 'sample_details' %>


<p><%= f.submit 'Submit' %></p>
<% end %>

<%= link_to 'Back to Batches', batches_path %>


# view of the partial I created as sample_details

<table>
  <tr>
    <th>Sample Name  </th>
    <th>Species  </th>
    <th>Sample type  </th>
    <th>Concentration</th>
    <th>Volume</th>
    <th>Sample comment</th>
  </tr>

<% (1..3).each do |index| %>
<% fields_for :samples do |ff| %>
<tr>   
    <td><%= ff.text_field :sname, :index => index, :size => 15 %></td>
    <td><%= ff.text_field :organism, :index => index, :size => 15 %></td>
    <td><%= ff.text_field :ini_conc, :index => index, :size => 3 %></td>
    <td><%= ff.text_field :ini_vol, :index => index, :size => 3 %></td>
    <td><%= ff.select(:samp_type, %w{ gDNA totalRNA cDNA}) %></td>
</tr>
<% end %>
<% end %>
</table>


model is the same as before..

Thanks in advance for helping

Re: Multiple rows of data with a single form

How would I do this in Rails 3.0.3 with a single model?


ryanb wrote:

You're right, there are some significant differences. Try this code.

def new
  @owners = Array.new(3) { Owner.new } # set up any defaults here
end

def create
  @owners = params[:owners].values.collect { |owner| Owner.new(owner) }
  if @owners.all?(&:valid?)
    @owners.each(&:save!)
    redirect_to :action => 'index'
  else
    render :action => 'new'
  end
end

# in new.rhtml
<%= start_form_tag :action => 'create' %>
<% @owners.each_with_index do |owner, index| %>
  <% fields_for "owners[#{index}]", owner do |f| %>
    First Name: <%= f.text_field :firstname  %>
    Last Name: <%= f.text_field :lastname  %>
  <% end %>
<% end %>
<%= end_form_tag %>


If you have any questions about the code feel free to ask.