Sending the details of the multi form to the database
I have to create a form which includes Batches and Samples.
General idea would be Batches having (received:date
, group_leader:string
, contact_person:string
, batch_comment:string
and num_of_samples:integer
) .
sample_name
, species
, sample_type
, ini_conc
, ini_vol
, and sample_comment
).
Now as these are of one to many relationships, I have created a form after following many tutorials having a page showing batch detail entry as well as asking for how many sample开发者_Go百科s it involved in each batch. After the user types in (for eg. 3), the form generates three fields having sample details (also including name
, species
, type
, conc
, vol
, etc.) but I was not able to send these details to the database.
New sample:
<p><% form_for(sample) do |f| %></p>
<p><% (1..@batch.sample_no).each do |i| -%></p>
<%= f.error_messages %>
<p>
<%= f.hidden_field :batch_id %>
</p>
<p>
<%= f.label :sample_name %><br />
<%= f.text_field :sname %>
</p>
<p>
<%= f.label :organism %><br />
<%= f.text_field :organism, :rows => 2 %>
</p>
<p>
<%= f.label :sample_type %><br />
<%= f.select(:samp_type, %w{ DNA RNA}) %>
</p>
<p>
<%= f.label :ini_conc %><br />
<%= f.text_field :ini_conc %>
</p>
<p>
<%= f.label :ini_vol %><br />
<%= f.text_field :ini_vol %>
</p>
<p>
<%= f.label :storage_space %><br />
<%= f.text_field :storage_space %>
</p>
<p>
<%= f.label :samp_comment %><br />
<%= f.text_area :samp_comment, :rows => 3 %>
</p>
<% end -%>
<p><%= f.submit 'Submit' %></p>
<% end %>
I haven't done anything much with my controller. (in fact I have no idea which controller I should add codes to) meaning I have created two scaffolds, one for batch and one for samples and I have made a partial of the samples_view_new.html.erb and saved it in the batch_view folder which gives me an opportunity to add samples in the show_html.erb
of the batch.
my models of batch.rb
and sample.rb
look some thing like this
<p>class Batch < ActiveRecord::Base</p>
<p>has_many :samples</p>
<p>end</p>
<p>class Sample < ActiveRecord::Base </p>
<p>belongs_to :batch</p>
<p>end</p>
if I understand your requirements correctly, you're searching for nested forms as excellently described by Ryan Bates at railscasts.com
Tutorial 1: Nested Model Form Part 1
Tutorial 2: Nested Model Form Part 2
Here is some more help for you:
In your Bash model
has_many :samples
accepts_nested_attributes_for :samples
Your Bash Controller will have at least 2 actions: new and create In your new action:
@bash = Bash.build
4.times { @bash.samples.build } # where 4 is the no. of samples, could also be param[:amount] in case you add a preliminary action with a form and an amount field, but I suggest using javascript as described in Tutorial 2 above for better user experience
In the create action:
@bash = Bash.new(params[:bash]) # you now have access to @bash.samples
if @bash.save ...
And finally the "new" view:
<%= form_for @bash do |f| %>
<%= f.text_field :group_leader %> <!-- ... -->
<%= f.fields_for :samples do |builder| %>
<%= builder.text_field :sample_name %> <!-- ... -->
<% end %>
<%= end %>
With this and the tutorials you should now be able to build a killer app for your degree's application. Good luck!
I think I have the solution to the problem...
My main idea was to send multiple rows in the database at once... so just follow this tutorial
http://www.stephenchu.com/2008/03/paramsfu-3-using-fieldsfor-and-index.html
精彩评论