Altering multiple rows into a model with one submit - ruby-on-rails

I'm trying to put together a rails site that uses a number of models. One is a model called requests that takes in a couple pieces of information associated with essentially a single purchase. The other is a model that collects a number of the requests together under one "order" id.
The problem is, I want the view associated with request to allow for multiple request entries with one submission, and I can't figure that out. As I've currently got it, someone can submit a single request for a single "cut", but I want them to enter a number of requests for a number of cuts on a single page, with a single submission.
My current _form document associated with my view is:
<%= form_for(#request) do |f*| %>
<div class="field">
<%= f.label :cut %><br />
<%= f.text_field :cut %>
</div>
<div class="field">
<%= f.label :lbs %><br />
<%= f.number_field :lbs %>
</div>
<div class="field">
<%= f.label :notes %><br />
<%= f.text_field :notes %>
</div>
<div class="field">
<%= f.label "Order ID" %><br />
<%= f.number_field :order_id %>
</div>
<div class="field">
<%= f.label :status %><br />
<%= f.number_field :status %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Any idea how I can create a form document that allows me to enter several new 'rows' of requests with one submit? Or how to ensure they all have the same order-id?
Thanks.

Try these screencasts from Ryanb, I think they'll get you a long way.
http://railscasts.com/episodes/196-nested-model-form-part-1
http://railscasts.com/episodes/197-nested-model-form-part-2

Related

Rails forms: how to store two text_fields in one array

Newbie to rails and on many online tutorials I see this example
<div class="field">
<%= f.label :first_name %><br />
<%= f.text_field :first_name %>
</div>
<div class="field">
<%= f.label :last_name %><br />
<%= f.text_field :last_name %>
</div>
which stores like this
"first_name"=>"foo", "last_name"=>"bar"
Is there a way use text_field or another form attribute to store two inputs in an array, so it would end up something like:
"name"=>["foo", "bar"]
This is not the preferred way, but curious to see how it would be done
Update
In your example you are passing #user.first_name and .last_name as the value. I was thinking more something more like this:
<div class="field">
<%= f.label "First Name" %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label "Last Name" %><br />
<%= f.text_field :name %>
</div>
where there is no #user.first_name but #user.name[0]
You can use text_field_tag to achieve this. (documentation: http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-text_field_tag)
A simple example would look like:
text_field_tag 'name[]', #user.first_name
text_field_tag 'name[]', #user.last_name
This will send both names under the name parameter. You can access the array with params[:name] in your controller.

submitted data vanishes after page refresh [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Associated model is not saving data when page is refreshed
I have a 1:1 association between User and Profile.
When I submit the new profile form, the data I've entered is displayed just fine (see screenshot: http://i.imgur.com/fY8YU.png), but when I refresh it the data is instantly wiped.
Could anyone tell me what is causing this?
Here's the submit form:
<%= form_for([#user, #user.build_profile]) do |f| %>
<div
class="field"> <%= f.label :first_name %><br /> <%=
f.text_field :first_name %> </div> <div
class="field"> <%= f.label :last_name %><br /> <%=
f.text_field :last_name %> </div> <div
class="field"> <%= f.label :picture %><br /> <%=
f.text_field :picture %> </div> <div class="field">
<%= f.radio_button(:sex, "male") %> <%=
f.label(:sex, "Male") %> <%= f.radio_button(:sex, "female")
%> <%= f.label(:sex, "Female") %> </div> <div
class="actions"> <%= f.submit %> </div> <%
end %>
Here's the users_controller: https://github.com/imjp/SuperModel/blob/master/app/controllers/users_controller.rb
Here's the profiles_controller: https://github.com/imjp/SuperModel/blob/master/app/controllers/profiles_controller.rb
Basically, it's failing because:
passing #user.build_profile instantiates a new profile each time.
sidenote but I highly recommend you create the profile just after the user is created.
Look at my commit on your forked repository.

Edit form does not auto-fill recently added columns

I've just added 2 new columns to my user table: photo and sex.
For some reason when I create a new user and fill up the entire form(name, photo and sex) it does not stay saved.
When I go to edit it just shows the name I've filled in, but the photo and sex fields are empty everytime.
Any idea what might be causing this?
Here's the form:
<div class="field">
<%= f.label :name, "Full Name" %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :photo_1, "Photo" %><br />
<%= f.text_field :photo_1 %>
</div>
<div class="field">
<%= f.radio_button(:sex, "male") %>
<%= f.label(:sex, "Male") %>
<%= f.radio_button(:sex, "female") %>
<%= f.label(:sex, "Female") %>
</div>
<div class="actions">
<%= f.submit %>
</div>`
Firstly I would rename that column from sex to gender.
Secondly check the attr_accessible declarations on your model, you may need to add these fields to the list

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) .
N.B batches have many samples.
now the Samples include ( 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 samples 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.
The only thing that updates the database is one sample information.
my view looks something like this :
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

One click of the submit button in form_for results in 3 items in my database

I have a form in my rails app that creates an item in my database, but when I submit the form, it creates 3 items per click.
I have 2 other forms that add things to the same database, but they are on different pages, could that be related?
This is my form on the "new debate" page:
<%= form_for(#debate) do |f| %>
<div class="field">
<%= f.label :proposition %><br />
<%= f.text_field :proposition %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I also have another form on the "show debate" page that appears twice:
<%= form_for(#debate.debates.create) do |support_form| %>
<div>
<%= support_form.label :content %><br />
<%= support_form.text_area :content %>
</div>
<%= support_form.hidden_field :is_supporting, :value => is_supporting %>
<div class="actions">
<%= support_form.submit %>
</div>
<% end %>
And when I click on the submit button on any of the 3 forms, I get 3 new debates.
I think your code, might be creating those extra records.
= form_for(#debate.debates.create) do |support_form|
If my assumption is correct .debates is an association, and you are creating that association with that line.
Try using build
= form_for(#debate.debates.build) do |support_form|

Resources