Rails nested model form wrong html id - ruby-on-rails

I have a Cover model which has many Slots. I created a form following the #196 Railcast and #197 Railcast.
The form works fine for creation, but when I want to update a Cover (specifically a child Slot) it doesn't get updated. The problem seems to be that the html name attribute of the fields for the slot do not contain the proper ID, instead, they always seems to start at 0 for the slots and increase. Here is the relevant code:
_form.html.erb:
<%= form_for(#cover) do |f| %>
...
<%= f.fields_for :slots do |builder| %>
<%= render "slot_fields", :f => builder %>
<% end %>
...
<% end %>
_slot_fields.html.erb:
<div class="fields">
<p>
<div class="field">
<%= f.label :width %>
<%= f.number_field :width %>
</div>
...
output html:
<p>
<div class="field">
<label for="cover_slots_attributes_0_width">Width</label>
<input id="cover_slots_attributes_0_width" name="cover[slots_attributes][0][width]" type="number" value="50" />
</div>
However the slot ID is 3
Thanks in advance

Related

Nested form keeps creating new forms instead of overwriting previous

I have a nested form that I render in my Submission show view that is meant to show a status field and notes field. This seems to be working fine, however whenever I navigate to the show view, it seems to show the current Status and Notes fields as well as creating additional Status and Notes fields. I would just like it to show one status and notes field that can be overwritten with new data.
Current form being rendered:
<%= form_for #submission do |f| %>
<%= f.fields_for :agent_activities do |a|%>
<td> <div class="field">
<%= a.text_field :Status%>
</div>
</td>
<td> <div class="field">
<%= a.text_field :Notes %>
</div>
</td>
<td>
<div class="actions">
<%= f.submit %>
</div>
</td>
<% end %>
<% end %>
Submission Controller:
def show
#submission.agent_activities.build
end
Solved.
This is a pluralized statement:
<%= f.fields_for :agent_activities do |a|%>
Needed to be singular like this,
<%= f.fields_for :agent_activity do |a|%>

Hidden value not being passed in rails app (Head first rails book)

I am following the Head first Rails book for creating an airline ticketing system although I am using version 3.2.13 of rails.
I have a hidden field for flight_id in a partial for adding new seats to a flight. This partial is then rendered on the flights show page. But when I view the outputted html the value for flight_id I get this,
<input type="hidden" value="flight_id" name="seat[flight_id]" id="seat_flight_id">
My code in the partial is
<%= form_for(seat) do |f| %>
<%= f.hidden_field :flight_id, value: :flight_id %>
<div class="field">
<%= f.label :baggage %><br />
<%= f.text_field :baggage %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
In my models I have
Flight.rb
has_many :seats
Seats.rb
belongs_to :flight
How do I pass the flight Id from the flight_id to the hidden field?
The flight_id is not available directly.
You can change this line
<%= f.hidden_field :flight_id, value: :flight_id %>
To
<%= f.hidden_field :flight_id, value: seat.flight.id %>

Rails - Use the same form with new/edit using fields_for

I have 5 models,
Person
person_car
car (has many tires)
person_car_tire
tire (belongs to car)
so I have this view
_form.html.erb
<%= form_for(#person) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div>
<% Car.all.each do |c|%>
<div class="field">
<%= f.fields_for(:person_cars) do |ff|%>
Car Name: <%= ff.check_box :car_id %>|<%= c.car_name%>
<% c.tires.each do |b|%>
<div class="field">
<%= ff.fields_for(:person_car_tires) do |fff|%>
Tire: <%#= fff.check_box :tire_id%>|<%= b.tire_name%>
<%end%>
</div>
<%end%>
<%end%>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
And when I save it works perfectly, the problem comes when I want to edit using this form because it duplicates data of each car 4 times in the view. I've been searching and fields for allows extra options so I made:
<%= form_for(#person) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div>
<% #person.cars.each do |c|%>
<div class="field">
<%= f.fields_for(:person_cars, c) do |ff|%>
Actividad: <%= ff.check_box :car_id %> | <%= c.car.name%>
<% c.person_car_tires.each do |t|%>
<div class="field">
<%= ff.fields_for(:person_car_tires, t) do |fff|%>
Tarea: <%#= fff.check_box :tire_id%>|<%#= t.tire.name%>
<%end%>
</div>
<%end%>
<%end%>
</div>
<%end%>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
and it now works, but only show the cars and tires that I've selected on the new action. not all as I wanted (because if I use the first form it duplicates checkboxes in the view).
How can I use the same form for both actions?
Hope someone can help me.
You can use the same _form partial for both new and edit. You just need to pass local variables set to different values to this form. Basically, whatever differs, abstract it away as a parameter (local variable) to this function-like partial.

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|

Rails: radio button selection for nested forms objects

I have the following form for photo_album which uses the nested forms feature of the Rails in saving the photos while updating the photo_album. And having trouble with the selection of radio button value. I want only one of the photos be able to select as the album cover, but due to the way rails produces form element ids and names, I am able to select all of the photos as album covers. Is there any workaround?
<% form_for #photo_album do |f| %>
<%= f.error_messages %>
<% #photo_album.photos.each do |photo| %>
<% f.fields_for :photos, photo do |photo_fields| %>
<p>
<%= image_tag url_for_image_column(photo, "data", :thumb) %>
</p>
<p>
<%= photo_fields.label :title %>
<%= photo_fields.text_field :title %>
</p>
<p>
<%= photo_fields.label :body %>
<%= photo_fields.text_area :body %>
</p>
<p>
<%= photo_fields.radio_button :cover, "1" %>
<%= photo_fields.label :cover, 'Album Cover', :class => 'option' %>
<%= photo_fields.check_box :_delete %>
<%= photo_fields.label :_delete, 'Delete', :class => 'option' %>
</p>
<% end %>
<% end %>
<p>
<%= f.submit #photo_album.new_record? ? 'Create' : 'Update' %>
</p>
<% end %>
And following is the html produced by rails (which is part of the problem) for radio buttons:
<p>
<input type="radio" value="1" name="photo_album[photos_attributes][0][cover]" id="photo_album_photos_attributes_0_cover_1"/>
<label for="photo_album_photos_attributes_0_cover" class="option">Album Cover</label>
<input type="hidden" value="0" name="photo_album[photos_attributes][0][_delete]"/><input type="checkbox" value="1" name="photo_album[photos_attributes][0][_delete]" id="photo_album_photos_attributes_0__delete"/>
<label for="photo_album_photos_attributes_0__delete" class="option">Delete</label>
</p>
I would guess that in this instance you'd want to use radio_button_tag instead and use the same name.
This should be close - you will still need to figure out when you want to pass in true:
<%= radio_button_tag "cover", photo.id, todo %>
You might also be able to specify the name with the radio_button helper, but it doesn't quite fit the model so radio_button_tag makes more sense to me - I could be mistaken.

Resources