Rails - Simple_fields_for not submitting multiple records - ruby-on-rails

I'm trying to create a form that allows a user to select different choices for each category on the same form, and each of these will create its own model. My problem is getting the form to submit multiple parameters. My code is as follows:
<%= simple_form_for choices_path do |f| %>
<td><%= person.fullname %></td>
<td><%= person.email %></td>
<td><%= person.phone %></td>
<% if Category.any? %>
<%= f.simple_fields_for :choices do |g| %>
<% Category.all.each do |category| %>
<td>
<%= g.input :item_id, as: :select, collection: booking.venue.menu_items(category) %>
<%= g.hidden_field :admin_id, value: person.id %>
</td>
<% end %>
<% end %>
<% end %>
<td><%= person.completed? %></td>
<td><%= f.submit %></td>
<% end %>
The idea obviously being to create a new form for each category. This works until submitting the data, where only the last form's data is submitted. My paramaters are as follows:
{"utf8"=>"✓", "authenticity_token"=>"tf3DSLGHyOVEVJkcLjSVE9HDJbjn1CIaYBJIfHFw6RYH8tcn0tNilWVIjzyvcjXYm2ovKNO5A31+TktGA8X2+Q==",
"/choices"=>{"choices"=>{"item_id"=>"Dessert 1", "admin_id"=>"3"}},
"commit"=>"Save /choices",
"venue_id"=>"1",
"booking_id"=>"26"}
I can see that its creating a hash and is ready to submit multiple choices, however it is only the last record's data that is submitted.
Any help on how to get multiple records to update is greatly appreciated.
Thanks in advance.

The simple form association helper might be what you are looking for:
link

Related

Rails form_for only submitting last input

Im trying to create inputs by looping each product and have one form submission for all the inputs. Currently, the form only submits the last input. How would I make it so all the inputs get submitted?
<%= form_for :inventory do |f| %>
<% #products.each do |product| %>
<tr>
<td><%= product.name %></td>
<td><%= product.measurement %></td>
<td><%= f.number_field :amount, class: 'form-control' %></td>
<%= f.hidden_field :product_id, :value => product.id %>
</tr>
<% end %>
<%= f.submit %>
<% end %>
I dont know your use case but you can use the gem cocoon for doing that. There will also be a link to add/remove another product.
https://github.com/nathanvda/cocoon
You can iterate with each_with_index:
<%= form_for :inventory do |f| %>
<% #products.each_with_index do |product, i| %>
<tr>
<%= f.hidden_field "product_id[#{i}]", :value => product.id %>
</tr>
<% end %>
<%= f.submit %>
<% end %>
Like that the output is not very elegant (something like "product_id"=>{"0"=>"1", "1"=>"2", "2"=>"3"... }) but it's for the example to show how every hidden field needs a unique key.
You can define your params in a better way to use them in the controller, just keep them unique.

update database with checkboxes - rails

So I have a users model which is associated to activities and activities are associated to a completed model?.
What I am trying to do is update if an activity is completed when a checkbox is clicked.
Here is my view:
<% #user.completeds.each do |task| %>
<tr>
<td><%= task.activity.name %></td>
<td><%= task.activity.type.name %></td>
<td><%= task.activity.points %></td>
<td><%= check_box_tag 'finished', '1', task.finished %></td>
</tr>
<% end %>
This all works great and shows if an activity is "finished" but I am having a hard time figuring out how to update the database with the checkbox. While I would like it to update when you click the checkbox I am ok with having a save button to save them all at once. Which ever would be easier probably would be best for me as I am just learning rails.
Thank you for any help.
Edit:
To clarify my question, my code above works for showing the data correctly, what I can't figure out is how to turn it into a form so that I can save any changes to the check box values.
The easiest solution would be to use a form and a save button. If you look at the Form Helpers Rails Guide, you can see an example:
<%= check_box_tag(:pet_dog) %>
<%= label_tag(:pet_dog, "I own a dog") %>
You probably want to use form_for, not form_tag though to update your object. See the documentation here.
<%= form_for task do |f| %>
<%= f.label :finished %>:
<%= f.check_box :finished %><br />
<%= f.submit %>
<% end %>
EDIT:
To update multiples, try something like this:
<% form_tag edit_multiple_products_path do %>
<table>
<tbody>
<% #user.completeds.each do |task| %>
<tr>
<td><%= task.activity.name %></td>
<td><%= task.activity.type.name %></td>
<td><%= task.activity.points %></td>
<td><%= check_box_tag 'finished', '1', task.finished %></td>
</tr>
<% end %>
</tbody>
</table>
<%= submit_tag "Update Tasks" %>
<% end %>
Except don't forget to do everything else that the RailsCast mentions.

Rails: How to DRY up multiple use of a partial

Like to DRY up the following code. Created a partial _user.html.erb, which is called either by the users view via
<%= render #users %>
and by the groups view via
<%= render partial: 'users/user', collection: #mailgroup.users, as: :user %>
The partial _user.html.erb is:
<%= content_tag_for(:tr, user) do %>
<td><%= user.id %></td>
<td><%= check_box_tag "user_ids[]", user.id, true %></td>
<td><%= user.firstname %></td>
<td><%= user.lastname %></td>
<td><%= user.function %></td>
<td><%= user.company %></td>
<td><%= user.appendix %></td>
<td><%= user.city %></td>
<td>
<%= link_to button1 ... %>
<%= link_to button2 ... %>
<%= link_to button3 ... %>
<%= link_to button4 ... %>
</td>
<% end %>
Now I like this partial having rendered once with all columns (call 1.), and also with a subset of columns (call 2.). Especially like to hide the check_box_tag in the second column.
I searched around and was confused at the end how to solve: With different layouts? How would I have to do it with partials? Or with checking first from what controller the call comes from? (This doesn't sound very satisfying to me).
In general: How to call the same partial with different subset of columns without maintaining different copies of that partial?
I'm not sure you can use layouts for partials and I agree that checking for controller source is a code smell. I would consider using another local to check if the fields are to be displayed which could be set in the "calling" view e.g.
<%= render #users, locals: {show_buttons: false} %>
<%= render partial: 'users/user', collection: #mailgroup.users, as: :user, show_buttons: true %>
and used in the user partial
<% if show_buttons %>
<td><%= check_box_tag "user_ids[]", user.id, true %></td>
<% end %>
You could dry this out further with a helper method
<td><%= user.id %></td>
<%= check_box(user, show_buttons)
module UsersHelper
def check_box(user, show_buttons)
if show_buttons
content_tag(:td) do
content_tag(:option, "user_ids[]", value: user.id )
end
end
end
end

fields_for with association question

How do I use association data from within a fields_for?
I've got a has many through relationship between users, schools and schools_users. A user can have multiple schools and a school has multiple users. In schools_users there is a user_role field in addition to the common user_id and school_id.
In the show view I have:
<% #school.schools_users.each do |user| %>
<tr><td><%= user.user.first_name %> <%= user.user.last_name %></td><td><%= user.user_role %></td></tr>
<% end %>
but I can't figure out how to do the same thing from within the fields_for on the edit page.
<%= f.fields_for :schools_users do |f| %>
<tr>
<td><%= NEED USER NAME HERE %></td>
<td><%= f.select(:user_role, active_role_options)%></td>
<td><%= link_to_remove_fields 'Delete', f %></td>
</tr>
<% end %>
Thanks for any help!
Firstly: You're assigning the block variable of your fields_for to f, which is the same f your form_for uses. You should use a different name, like user_fields.
With that:
<%= f.fields_for :schools_users do |user_fields| %>
<tr>
<td><%= user_fields.text_field :first_name %></td>
...
<% end %>

Rails 3 submit a form with multiple records

I'm new to rails so this is probably a basic question. I am trying to create a form where the user can create 3 records at once. I want the user to only have to click the submit button once. I'm submitting to my Review model a name, comment, and rating. Currently, only the last record is entered into the database.
<%= form_for([#user,#review]) do |f| %>
<table>
<tr>
<td>Rank</td>
<td>Name</td>
<td>Comment</td>
</tr>
<tr>
<td>1</td>
<td><%= f.text_field :name %></td>
<td><%= f.text_field :comment %></td>
<%= f.hidden_field :rating, :value=> "5" %>
</tr>
<tr>
<td>2</td>
<td><%= f.text_field :name %></td>
<td><%= f.text_field :comment %></td>
<%= f.hidden_field :rating, :value=> "3" %>
</tr>
<tr>
<td>3</td>
<td><%= f.text_field :name %></td>
<td><%= f.text_field :comment %></td>
<%= f.hidden_field :rating, :value=> "1" %>
</tr>
</table>
<div class="actions">
<%= f.submit "Create my top 3" %>
</div>
<% end %>
Any advice is appreciated. Thanks.
I would recommend using fields_for for this:
<%= form_for([#user, :reviews]) do |f| %>
<% #reviews.each do |review| %>
<%= fields_for review do |r| %>
<%= render "reviews/form", :r => r %>
<% end %>
<% end %>
<% end %>
To make this work, you will need to build as many review objects as you require in your controller:
def new
# you could also have this in a before_filter...
#user = User.find(params[:id])
#reviews = Array.new(3) { #user.reviews.build }
end
This would create new instances of review records for this user, which is different from new records. Instances are simply Ruby objects. Now because you've called #user.reviews.build three times, you'll see three reviews in your view.
def create
#user = User.find(params[:id])
#reviews = Review.create(params[:reviews])
# Some more logic for validating the parameters passed in
end
This will create three new Review objects and link them to #user, assuming all three are valid.
You'll need to tell rails its an array. First, read this section of this article:
For your purpose, you'll need to build the form by hand:
<%= form_tag 'foo' do %>
<% [1,3,5].each do |i| %>
<%= text_field_tag 'review[][name]' %>
<%= text_field_tag 'review[][comment]' %>
<%= hidden_field_tag 'review[][rating]', :value => i %>
<% end %>
<% end %>

Resources