I'm trying to create a select box that takes data from my db. I'm having trouble setting this up. I tried this code:
<%= f.fields_for :unit do |u| %>
<%= u.label :name %>
<%= u.select :name, :class => "ingredient_unit", :prompt => "Please Select" %>
<% end %>
but I'm missing the part of the choices, I don't know how to pull them out of the database. I tried using collection_select, which worked, but then the class option wasn't working... collection_select went like this:
<%= u.collection_select :unit, Unit.all, :id, :name, :class => "ingredient_unit", :prompt => "Please Select" %>
I also don't understand what the first symbol means (:unit), it seems to be setting the html id and name, so that can be anything I want it to be?
You should look at the documentation for collection_select and select. But to answer your question, for the select part, you forgot to pass the list of options to choose from. You also need to swap the order for prompt and class since prompt is an option for the helper and class is an html option
<%= u.select :unit_id, Unit.all.map { |u| [u.name, u.id] }, { :prompt => "Please Select" }, { :class => "ingredient_unit" } %>
For the collection select
<%= u.collection_select :unit_id, Unit.all, :id, :name, { :prompt => "Please Select" }, { :class => "ingredient_unit" } %>
The first parameter passed to both helper is the column name where you want the selected answer to be saved. The 2 codes above just shows 2 different ways to generate the same select tag.
The first symbol tells it which field to populate with the id returned from the user selection.
Also, you should wrap your class section in {}
:unit refers to the model attribute that you're using for the select element. Yes, it will setup the name/id of the element (and name is the most important for the params hash).
To set a class in the collection_select, specify it as a hash as that helper takes it as an html_option.
<%= u.collection_select :unit, Unit.all, :id, :name, { :prompt => "Please Select" }, { :class => "ingredient_unit" } %>
Related
I have found a few similar articles on stack but none of these examples seem to do the trick.
I'm calling map on an object, to create options in an f.select field, and I'm also using Include_blank option, then trying the class and the class will not work.
I've tried the following:
<%= f.select(:city_race_id, #city_races.map {|n| [n.office, n.id]}, include_blank: true, html: { class: "form-control" }) %>
and
<%= f.select(:city_race_id, #city_races.map {|n| [n.office, n.id]}, include_blank: true, :class => 'form-control' ) %>
both render form and doesn't break rails, but neither show styling.
Try this way :
<%= f.select(:city_race_id, #city_races.map {|n| [n.office, n.id]}, {include_blank: "Select something"}, { :class => 'form-control' }) %>
select helper takes two options hashes, one for select, and the second for html options. you can also add prompt option.
I would like to have a "select an option" option in my dropdown on my rails application.
I am using the collection_select helper tag and it looks something like this:
<%= collection_select(:country, :id, Country.order('name ASC'), :id,:name, {},{:class => "input-xlarge"}) %>
I want the default option of the drop-down to be "Select a country".
Use the include_blank option:
<%= collection_select(:country, :id, Country.order('name ASC'),
:id, :name,
{ include_blank: 'Select a country' },
{ :class => "input-xlarge" }) %>
See the official documentation here
I am editing a Rails 2 application. In it, a user submits a form that includes a dropdown menu, and that information creates a record in my database. When the user goes to edit the record, I want to show the saved "selected" option in the dropdown menu. I keep getting errors, however. Here is my set-up:
View Select Field
<% form_for #newsavedmap, :html=>{:id=>'createaMap'} do |f| %>
<%= f.select :start, options_for_select(#itinerary.locations), {:include_blank => true}, {:id=>"startdrop" } %>
Form Helper
def options_for_select(locations)
locations.map do |l|
content_tag "option", l.masterlocation.name, location_option_attributes(l)
end.join("\n")
end
private
def location_option_attributes(location)
{
:value => "#{location.masterlocation.street_address}, #{location.masterlocation.city}, #{location.masterlocation.state}, #{location.masterlocation.zip}",
:id => location.masterlocation.id,
:"data-masterlocation-name" => location.masterlocation.name,
:"data-masterlocation-id" => location.masterlocation.id,
:"data-masterlocation-latitude" => location.masterlocation.latitude,
:"data-masterlocation-longitude" => location.masterlocation.longitude
}
end
I have tried making the view look like this:
<%= f.select :start, options_for_select(#itinerary.locations, #newsavedmap.start), {:include_blank => true}, {:id=>"startdrop" } %>
But I get wrong number of arguments (2 for 1)) for that line. Also tried
<%= f.select :start, options_for_select(#itinerary.locations), {:selected => #newsavedmap.start, :include_blank => true}, {:id=>"startdrop" } %>
But nothing is preselected when I go to edit the saved map. I've tried following these links, and keep striking out. Appreciate any help you have to offer!
Rails select helper - Default selected value, how? , Rails form_for select tag with option selected , Rails select helper - Default selected value, how?
You could try something like this in your helper
def options_for_select(locations, selected=nil)
locations.map do |l|
tag_options = location_option_attributes(l)
if l == selected
tag_options[:selected] = "selected"
end
content_tag "option", l.masterlocation.name, tag_options
end.join("\n")
end
then call field like you were trying before.
<%= f.select :start, options_for_select(#itinerary.locations, #newsavedmap.start), {:include_blank => true}, {:id=>"startdrop" } %>
You can pass one more parameters to select the value like this:
<%= f.select :start, options_for_select(#itinerary.locations), :selected => #newsavedmap.start, {:include_blank => true}, {:id=>"startdrop" } %>
I have a collection_select form with the following code:
<%= f.collection_select(:city_id, City.order('name ASC'), :id, :name, {:prompt => "Select a City"}, {:id => 'cities_select'}) %>
Right now it just lists all of the cities by name in my database, but there are duplicate city names. For example, there are two "Peoria" cities.
So, I want to show the state attribute separate by a comma like: "Peoria, IL" to help distinguish the cities. How do I write this code within the form code?
You can also use collection_select for that
f.collection_select :city_id, City.order('name ASC'), :id, :name_with_state, { prompt: 'Select a City' }, { id: 'cities_select' }
name_with_state should be in your city model
def name_with_state
"#{name}, #{state}"
end
f.select :city_id, City.all.map{|c| ["#{c.name}, #{c.state}", c.id] }, { :prompt => "Select City" }
You can use select:
<%= f.select(:city_id, City.order('name ASC').map{ |city| [city.your_method, city.id]},
{:prompt => "Select a City"}, {:id => 'cities_select'}) %>
I'm trying to create a <select> element using the collection_select method, but it seems that in order for the proper <option> to be selected, the identifier passed into collection_select needs to be an instance variable and not a local variable (this is happening in a partial).
So when I create a <select> for the categories of a product, the proper category is NOT selected by default.
_product_row.erb (DOES NOT WORK):
My product: <%= product.name %>
<%= collection_select(:product, :category_id, #current_user.categories, :id, :name, options = {:prompt => "-- Select a category --"}) %>
Screenshot:
alt text http://img534.imageshack.us/img534/8929/screenshot20100421at120.png
I discovered that I was able to get it to work by declaring an instance variable before hand, but this seems like a huge hack to me.
_product_row.erb (WORKS):
<% #product_select_tmp = product %>
<%= collection_select(:product_select_tmp, :category_id, #current_user.categories, :id, :name, options = {:prompt => "-- Select a category --"}) %>
Screenshot:
alt text http://img534.imageshack.us/img534/1958/screenshot20100421at120l.png
Because this partial is iterating over a collection of products, I can't just have #product declared in the controller (IOW unless I'm missing something, product must be a local variable in this partial).
So how do I get collection_select to select the appropriate item when calling it with a local variable?
Have you tried passing in the :selected key in the options hash? If you provide it with the current product.id it should behave the way you're expecting.
<%= collection_select(:product, :category_id, #current_user.categories, :id, :name, {:prompt => "-- Select a category --", :selected => product.category.id}) %>
You can pass collections to partials and designate a local variable to pass them as:
<%= render :partial => "products/product_row", :collection => #products, :as => :products %>
Relevant documentation: http://apidock.com/rails/ActionView/Partials