Rails: radio button selection for nested forms objects - ruby-on-rails

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.

Related

Displaying an objects properties different from default behavior in form helper rails

How can I go about displaying the :cords property below as a formatted array itself? and not just it's values. Given the form below:
<%= form_for #group do |f| %>
<p>
<%= f.label :name %><br>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :cords %><br>
<%= f.text_field :cords, name: "group[cords]" %>
</p>
<p>
<%= f.label :members %><br>
<%= f.text_field :members %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
When editing the object, I'd get something like
But what I want to see is the full raw data, like so:
I think this can be done with to_s, but i'm not sure how to go about getting this behavior
You could try using an input tag instead text_field helper:
<input type="text" name="group[cords]" value="<%= group.cords %>">

Rails - how to save options chosen by user

I have two models: Meal and Tag. They are associated by HABTM. What I want to do is to add new meal. So I have new.html where is name field, preparation field, and all tags (represented by toggle-buttons - every tag has own toggle-button). User can clicked tags, which want to save in this meal.
And here is a problem. I can display all tags, but I have no idea how can I tell rails to save only clicked tags.
Could you help me find right approach?
oh, and there is a user too (user has many meals), but I thing it doesn't matter.
Here is my view:
<%= form_for(#meal,:html => {:class => "meal-data-form"}) do |f| %>
<%= render 'shared/error_messages', object: f.object%>
<%= f.label :name, "Nazwa *" %>
<%= f.text_field :name, class: 'form-control'%>
<%= f.label :preparation, "Sposób przyrządzenia" %>
<%= f.text_area :preparation, class: 'form-control' %>
<h3>Określ tagi dla tego posiłku</h3>
<div id="tags-associated-with-meal">
<h4>Rodzaj posiłku</h4>
<div id="associated-nutrient-tags">
<% current_user.nutrient_tags.each do |nutrient_tag| %>
<button id="associated-type-tag-<%= nutrient_tag.id %>" type="button" class="btn btn-primary" data-toggle="button" aria-pressed="false" autocomplete="off">
<%= nutrient_tag.name %>
</button>
<% end %>
</div>
</div>
<%= f.submit yield(:button_name), class: "btn btn-primary" %>
I would not use a toggle button to do this, I would probably use checkboxes.
This is what you would do if you used checkboxes:
<%= f.collection_check_boxes :nutrient_tags_ids, current_user.nutrient_tags, :id, :name do |b| %>
<div class="collection-check-box">
<%= b.check_box %>
<%= b.label %>
</div>
Here is something for reference: http://www.sitepoint.com/save-multiple-checkbox-values-database-rails/
And here is the Rails collection_check_boxes official documentation: http://apidock.com/rails/v4.0.2/ActionView/Helpers/FormOptionsHelper/collection_check_boxes
Hope that helps.

Ruby on Rails go to a specific page when clicking "f.submit"

In my Ruby on Rails application I have the following form for booking_line view:
<%= form_for #booking_line do |f| %>
<%= f.hidden_field :showing_id %>
<%= image_tag "thor_hammer.jpg",:size => "900x250" %>
<td width="300px"> <br><%= f.label :seat_id, 'Please Select a Seat:' %>
<br><%= f.collection_select :seat_id, free_seats, :id,:seats_available %><br>
</td>
<td width="300px">
<div class="actions">
<%= f.submit %>
</div>
<br>
<%= render "/error_messages", :message_header => "Cannot save: ", :target => #booking_line %>
</td>
<% end %>
But as this is booking_line, what I want to happen is that when the submit button is clicked it creates a booking and a booking_line because in the booking_line table it has the attribute booking_id so that the association is made between the two. How can I achieve this?
you can use url in form_for method..to overwrite individual conventions, such as:
<%= form_for(#post, url: my_custom_path) do |f| %>
...
<% end %>
and then add your own logic at my_custom action to achieve it

Rails nested model form wrong html id

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

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.

Resources