Rails - creating place with weekdays in one form - ruby-on-rails

I created a model for a Place
class Place < ApplicationRecord
has_many :open_days, dependent: :destroy
end
and a model for OpenDay:
class OpenDay < ApplicationRecord
belongs_to :place
end
I want to be able to create a record of this place (what I have now is simple textfields) with day of the weeks (and hours) that the place is opened at.
My current form:
<%= form_for(#place) do |f| %>
<%= f.label(:name) %>
<%= f.text_field(:name, placeholder: "Place's name", class: "form-control") %>
<%= f.label(:street) %>
<%= f.text_field(:street, placeholder: "Street", class: "form-control") %>
<%= f.fields_for :open_days do |open_day| %>
<%= open_day.text_field :day %>
<% end %>
....
<% end %>
My new controller
def new
#place = Place.new
7.times do
#place.open_days.build
end
end
I decided to go with a table (code below) but I have absolutely no idea how to create a form for another model inside my existing form for #place. And what's more to be able to save multiple records using this form. Searched through SO but came with noting.
EDIT
I somehow was able to do is, but now there is this problem:
....
<tbody>
<tr>
<th>Open?</th>
<%= f.fields_for :open_days do |o_day| %>
<td><%= o_day.text_field :day, class: "form-control" %></td>
<% end %>
</tr>
<tr>
<th>From:</th>
<%= f.fields_for :open_days do |o_day| %>
<td><%= o_day.text_field :from_time, class: "form-control" %></td>
<% end %>
</tr>
<tr>
<th>To:</th>
<%= f.fields_for :open_days do |o_day| %>
<td><%= o_day.text_field :to_time, class: "form-control" %></td>
<% end %>
</tr>
</tbody>
First table row is populated with input with names like
name="place[open_days_attributes][0][day]"
name="place[open_days_attributes][1][day]"
name="place[open_days_attributes][2][day]"
name="place[open_days_attributes][3][day]"
name="place[open_days_attributes][4][day]"
name="place[open_days_attributes][5][day]"
name="place[open_days_attributes][6][day]"
I would expect the next row to start also from 0 to be like this:
name="place[open_days_attributes][0][from_time]
but instead it is like this:
name="place[open_days_attributes][7][from_time]
How to change it to be iterating from 0 again?

you should use nested attributes and form_for#fields_for
http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for

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.

Rails - Simple_fields_for not submitting multiple records

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

Rails - update multiple resource entries with one link

I have a table of forms
<% #group.lessons.each do |lesson| %>
<%= form_for [#group, lesson] do |f| %>
<tr id='<%= lesson.id%>' >
<td><%= f.text_field :time %></td>
<td><%= f.text_field :day %></td>
<td><%= f.text_field :subject %></td>
<td><%= f.text_field :teacher %></td>
<td><%= f.text_field :room %></td>
<td><%= f.submit 'Update'%></td>
<td><%= link_to 'Delete', [lesson.group, lesson], method: :delete%></td>
</tr>
<%end%>
<%end%>
Each form updates an entry when the "update" button is clicked. But when you edit two entries and update only one, the info you edited in the other one is gone.
I want to have a button to update each entry in the table. How do I do this?
UPDATED:
First in model:
class Group < AR::Base # possibly you'r using ActiveRecord
attr_accessible :lessons_attributes
accepts_nested_attributes_for :lessons, :allow_destroy => true
has_many :lessons
end
and then in your view: # e.g. views/groups/_form.html.erb
<table>
<%= form_for #group do |f| %>
<%= f.error_messages %>
<%= f.fields_for :lessons do |lesson_form| %>
<%= render "lessons/lesson", :f => lesson_form%>
<% end %>
<tr><td><%= f.submit 'Update'%></td></tr>
<% end %>
</table>
and in views/lessons/_lesson.html.erb
<tr>
<td>
<%= f.text_field :subject %>
<%= f.check_box :_destroy %>
<%= f.label :_destroy, "Remove Lesson" %>
</td>
</tr>
I think this is more of an html issue than a rails issue. You simply can't submit more than one form using plain html. That's why you're losing one form when you submit the other.
What you could do however is update multiple lessons that belong to the same group using nested attributes. Here are some resources for that:
http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
http://railscasts.com/episodes/196-nested-model-form-part-1
https://github.com/ryanb/nested_form

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