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 ))%>
Related
Following is my code for select tag.
I want to disable the option that has been selected
How to achieve this?
<td>
<%= select("gu", "rm_id", Rm.where(category_id: Admin.select("category_id").where(id: current_user.id)).collect {|p| [ p.name, p.id ] }, {prompt: 'Assign RM'},{onchange: "update_rm_div(this.value,#{gu.id})"}) %>
</td>
Any help is appreciatable
I have the following in a form:
<div class="col-sm-2 form-group">
<%= f.label :from_age, "From Age" %><br>
<%= f.select :from_age, [*-1..65], {}, class: "form-control" %>
</div>
<div class="col-sm-2 form-group">
<%= f.label :to_age, "To Age" %><br>
<%= f.select :to_age, [*-1..65] << 1000, {}, class: "form-control" %>
</div>
In other words:
For from_age, the user selects an age between the numbers -1 through 65. -1 represents the concept of "All".
For to_age, the user selects an age between the numbers -1 through 65, and also can choose the number 1000. -1 represents the concept of "All", and 1000 represents the concept of "and up".
With the from_age and to_age specified, The app can now say:
An agency serves individuals of All Ages
An agency serves individuals of ages 18 and up
An agency services individuals of ages 25-40
When creating a new agency, the above code works ok, but the user needs to know that the value -1 stands for up, and the value 1000 stands for the value and up.
I can do better. I just need to have text that represents the values within the select box, So within the select box: instead of seeing -1 the user sees: All, and instead of 1000 the user sees: and up.
I do have the following in my Agency model:
def from_age_to_s
if from_age == -1
return "All"
else
return from_age
end
end
def to_age_to_s
case to_age
when -1
"All"
when 1000
"+up"
else
to_age
end
end
So basically I need to tell the form: "Yes, those are the values and are correct, but for the displayed text for those values, utilize these methods for the text that represents those values that get passed through the form.
Documentation on select form helper.
Perhaps in this situation it is better to use collection_select?
Update:
Thanks to the help provided by the answer below. Here is my solution for from_age:
<div class="col-sm-2 form-group">
<%= f.label :from_age, "From Age" %><br>
<%= f.select :from_age, options_for_from_date, {}, class: "form-control" %>
</div>
# helpers/application_helper.rb
def options_for_agencies_from_date
[ [ 'All', -1 ], *0..65]
end
def options_for_agencies_to_date
[ [ 'All', -1 ], *0..65, [ '+up', 1000 ] ]
end
You can pass an array like [ [ 'Label 1', 'value 1' ], [ 'Label 2', 'value 2' ], ... ] to select, which will result in options like these:
<option value="value 1">Label 1</option>
<option value="value 2">Label 2</option>
...
But there's also a shortcut we can use: Not every element in the choices array has to be a [ label, value ] array like above. For elements that aren't arrays, select will just use the value for both the label and the value. In other words, if we do this:
choices = [ [ 'Foo', 1 ], 2, 3, [ 'Bar', 4 ] ]
...then select will render options like this:
<option value="1">Foo</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">Bar</option>
Armed with this knowledge, we can generate an array with the labels and values we want (you'll probably want to make it a constant at the top of your controller so it's only calculated once):
class MyController < ApplicationController
AGE_CHOICES = [ [ 'All', -1 ], *0..65, [ '+up', 1000 ] ]
# ...
end
puts MyController::AGE_CHOICES
# => [ [ 'All', -1 ],
# 0,
# 1,
# ...
# 65,
# [ '+up', 1000 ] ]
Now in your view you can just do this:
<%= f.select :from_age, MyController::AGE_CHOICES, {}, class: "form-control" %>
...which will render something like this:
<option value="-1">All</option>
<option value="0">0</option>
<option value="1">1</option>
...
<option value="1000">+up</option>
I have a select_tag in ruby on rails. The syntax for this is,
<%= select_tag "iso_region", options_for_select(#all_regions.collect {|p| [ "#{p['cc']}-#{p['lr']}", p['cc'] ] }), class: "form-control selectpicker reg_name", :data => {:'live-search' => 'true'} %>
Sample Options generated is like this,
<option value="ET">ET-Africa</option>
<option value="NG">NG-Africa</option>
<option value="PG">PG-Pacific</option>
<option value="IT">IT-Europe</option>
And I want IT-Europe to be selected in the dropdown.
How can I do that with my select_tag?
Try this:
options_for_select(#all_regions.collect {|p| [ "#{p['cc']}-#{p['lr']}", p['cc'] ] }, "IT"),
The last arg you pass to options_for_select is the value to be marked selected when it's rendered.
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
What if I need to add a class to the options tag?
<option value="testme1" class="selected">testme1</option>
I haven't been able to do this.
This is what I have
<%= select(:user, :goalstext, Bodytarget.all.collect {|b| [ b.name, b.id ] },{"class", "test"}, :class => "selected", :class => "selected") %>
Try this:
<%= select(:user, :goalstext, Bodytarget.all.collect {|b| [ b.name, b.id ] },
{}, {:class => "selected"}) %>
Remember the params' order:
select(object, method, choices, options = {}, html_options = {})
EDIT
I'm sorry, I read your question too fast. My code will add class to the select, not the options. I'll check how to do what you want... sorry!