Passing in select menu into nested form's attributes - ruby-on-rails

I have a nested form that has a select dropdown menu.
How can I pass the selected item's ID into the attributes that are submitted when the 'submit' button is hit?
In my situation, this is what my nested form's partial looks like:
=select("dish", "menu_id", Menu.all.collect {|r| [ r.name, r.id ] }, {:include_blank => 'Choose a Menu'})
=f.hidden_field :_destroy
=f.text_field :name, placeholder: "Name"
=f.text_field :price, placeholder: "Price"
=link_to "X", '#', :class => "remove_fields"
When I hit the submit button, the Terminal/Console displays that the following are being submitted to the database but you'll notice that the menu_id from the dropdown menu isn't part of the dishes_attributes since it doesn't share the same input id and name as the other two fields.
... "dishes_attributes"=>{"1342759486320"=>{"_destroy"=>"false", "name"=>"Dish 1", "price"=>"32"} ...
From the HTML source code, the name and price input fields get assigned the following id and name:
<input id="side_dish_dishes_attributes_1342759902918_name" name="side_dish[dishes_attributes][1342759902918][name]" placeholder="Name" size="30" type="text"/>
<input id="side_dish_dishes_attributes_1342759902918_price" name="side_dish[dishes_attributes][1342759902918][price]" placeholder="Price" size="30" type="text"/>
But my select dropdown looks like this:
<select id="dish_menu_id" name="dish[menu_id]">
<option value="">Choose a Menu</option>
<option value="1">Menu 1</option>
<option value="2">Menu 2</option>
</select>
In the parent _form page, here's what the call looks like that then calls this partial:
=link_to_add_fields "Add New Dish", f, :dishes
With the link_to_add_fields looking like this from my helpers/application_helper.rb:
def link_to_add_fields(name, f, association)
new_object = f.object.send(association).klass.new
id = new_object.object_id
fields = f.fields_for(association, new_object, child_index: id) do |builder|
render(association.to_s.singularize + "_fields", f: builder)
end
link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")})
end

Instead of this line
=select("dish", "menu_id", Menu.all.collect {|r| [ r.name, r.id ] }, {:include_blank => 'Choose a Menu'})
use it with form object
=f.select("menu_id", Menu.all.collect {|r| [ r.name, r.id ] }, {:include_blank => 'Choose a Menu'})
I hope this will sort out your id problem as well as part of dishes attributes

Related

Rails form_for multiple collection_select, values not selected on form failure

I'm not sure why, but my form is not showing the options selected on submit, even though the params hash shows that the information is being returned to the page.
Collection select code:
<%= f.collection_select :post_topic_ids, PostTopic.all, :id, :name, {}, { multiple: true, class: 'form-control' } %>
Which renders:
<select multiple="multiple" class="form-control" name="post[post_topic_ids][]" id="post_post_topic_ids">
<option value="1">Psychology</option>
<option value="2">Engineering</option>
<option value="3">Nanotechnology</option>
</select>
Params returned after form validation error
params = {"post"=>{"post_topic_ids"=>["", "1"]}}
Update
I have also tried:
<%= select_tag 'post_topic_ids', options_for_select(PostTopic.all.collect{ |p| [p.name, p.id] }), multiple: true %>
and:
<%= select_tag 'post_topic_ids', options_from_collection_for_select(PostTopic.all, "id", "name"), multiple: true %>
Which renders:
<select name="post_topic_ids[]" id="post_topic_ids" multiple="multiple"><option value="1">Psychology</option>
<option value="2">Engineering</option>
<option value="3">Nanotechnology</option></select>
you need to specify which element is selected a third parameter
<%= select_tag 'post_topic_ids', options_for_select(PostTopic.all.collect{ |p| [p.name, p.id] }, --->selected_element<---), multiple: true %>
look at http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_for_select for some examples.

Display selected option when editing a form (Rails 4)

I want to display my dropdown form selected option value whenever an user edits the form. Right now it displays the first option (in this case a blank option).
Form
<%= f.select(:condition, options_for_select([["Brand New", "Brand New"], ["Pre-owned", "Pre-owned"]]), :include_blank => true, :selected => params[:condition]) %>
HTML Output
<select id="product_condition" name="product[condition]">
<option value=""></option>
<option value="Brand New">Brand New</option>
<option value="Pre-owned">Pre-owned</option>
</select>
JSON
{
id: 2,
condition: "Pre-owned",
}
Thanks.
<%= f.select(:condition, options_for_select([["Brand New", "Brand New"], ["Pre-owned", "Pre-owned"]], :selected => f.object.condition), :include_blank => true) %>
Check the options_for_select documentation, and you will discover that the last parameter is the selected option.
options_for_select(container, selected = nil)
In your case
<%= f.select(:condition, options_for_select([["Brand New", "Brand New"], ["Pre-owned", "Pre-owned"]], params[:condition]), :include_blank => true) %>
assuming params[:condition] contains the currently selected value, and the value matches the corresponding value in the select tag.
In other words, for "Pre-owned" to be selected, params[:condition] must contain "Pre-owned".
<%= f.select :condition, options_for_select([["Brand New", "Brand New"], ["Pre-owned", "Pre-owned"]], f.object.condition), {:include_blank => true} %>
<%= form.label :Hobbies%>
<%= form.select(:hobbies, options_for_select(%w[cricket football hockey
reading], :selected => form.object.hobbies )) %><br><br>
This form multiple drop down but user only select one and if you edit and show method show only one record.

Add value to select in rails

i have a select that take values from database.
<%= select_tag :location, options_from_collection_for_select(Country.all, :id, :country_name), { :class => 'selectpicker' } %>
So i get all countries from database.
How i can do for add a custom value(for example Any, with value 0), to this select list taken from database ?
For example now i have:
<select>
<option value="UK">UK</option>
</select>
and i want get this:
<select>
<option value="0">Any</option>
<option value="UK">UK</option>
</select>
Thanks.
You can use the either prompt or include_blank options (FormOptionsHelper) as follows:
<%= select_tag :location, options_from_collection_for_select(Country.all, :id, :country_name), :prompt => 'Any', :class => 'selectpicker' %>

rails multiple checkboxes in 1 db column. saves correctly but doesn't show as checked

Event saves the section id's in an array. But in the /events/1/edit view for a newly created event, the expected checkboxes are not checked. I'm guessing because the default for checkbox values is booleans.
Event.last.newsletters #=> ["108", "115", "116", "117", "118", ""]
I have a CRUD for Event. Each Event can belong to multiple Sections. I have this displayed as a collection of checkboxes with the simpleform gem.
<%= simple_form_for #event do |f| %>
...
<%= f.collection_check_boxes :newsletters, Section.all, :id, :name, :input_html => { :class => 'checkbox' } %>
This results in the following html:
<span><input id="event_newsletters_1" name="event[newsletters][]" type="checkbox" value="1" /><label class="collection_check_boxes" for="event_newsletters_1">Newsletter 1</label></span>
<span><input id="event_newsletters_2" name="event[newsletters][]" type="checkbox" value="2" /><label class="collection_check_boxes" for="event_newsletters_2">Newsletter 2</label></span>
etc. etc.
When I create a new event or edit an event, the newsletter values are saved properly in the model.
Try specify the param :checked:
<%= f.collection_check_boxes :newsletters,
Section.all,
:id,
:name,
:input_html => { :class => 'checkbox' },
:checked => #event.newsletters %>
Reference: how to preselect an association checkbox using simple_form
If you are using SimpleForm you can use this:
<%= f.input :newsletters, collection: Section.all, as: :check_boxes %>

Ruby on rails: Select options menu with default value attribute

I need to produce a select menu with a Default value on the list of <options> . Here is how I need it looks like.
<select name="menu[parent_id]" id="menu_parent_id">
<option value="0">==None==</option>
<option value="34">TEST</option>
</select>
Currently I use this select helper in my form
<%= f.select(:parent_id, #parent_menus.collect {|p| [ p.name, p.id ] }, {:include_blank => '==None=='})%>
the above code produce this; (value="")
<select name="menu[parent_id]" id="menu_parent_id">
<option value="">==None==</option>
<option value="34">TEST</option>
</select>
Does anyone here can show me a way to add value="0" to the options list?
<%= f.select(:parent_id, [["==None==", 0]] + #parent_menus.collect {|p| [ p.name, p.id ] }) %>
Try
<%= f.select(:parent_id, options_for_select(["==None==", 0] + #parent_menus.collect {|p| [ p.name, p.id ] }, 0)) %>
Thought I would add this to anyone looking to do a default selected value that is one of the objects in the dropdown, as opposed to a 'none' value. ie, you are editing a form that has a previous value selected, and you don't want to lose that previous value on your edit page:
Assuming you have an array of parents held in #parents and your form is tied to an object called #my_messed_up_family, and #my_messed_up_family has one 'father':
= f.label :parent_id, "Choose which of your parents is your father?
= f.select :parent_id, options_from_collection_for_select(#parents.sort_by {|n| n.name}, "id", "name", :selected=>#my_messed_up_family.father.id)
I don't know this is Ruby way or not But this will definietly work
<%= f.select(:parent_id, "<option value='0'>Please select</option>"+options_for_select(#parent_menus.collect {|p| [ p.name, p.id ] }))%>
EDITED. For pre-selected according to the value save in database i assume #user is your object contain the database value for following example.
<%= f.select(:parent_id, "<option value='0'>Please select</option>"+options_for_select(#parent_menus.collect {|p| [ p.name, p.id ] }, #user.id ))%>

Resources