How to add html class to rails f.select field? - ruby-on-rails

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.

Related

Rails: Submit form on change

how can I get this to work?
<%= form_for current_user, html: {multipart: true } do |f| %>
<%= f.select(:brand, Brand.pluck(:company), {prompt:true}, {class: 'form-control'}, {:onchange => 'this.form.submit()'}) %>
<% end %>
The goal is to submit the form on change automatically. But the code above does not work. I get an wrong number of arguments (5 for 1..4) error. Any ideas?
The last three arguments there are actually a hash of options. Try putting them into curly braces to make it more clear, if you like:
<%= f.select(:brand, Brand.pluck(:company), {
prompt: true,
class: 'form-control',
onchange: 'this.form.submit()'
}) %>
Got it. Removed {prompt:true} and it works!
Select Helper:
select(object, method, choices = nil, options = {}, html_options = {}, &block)
This should work with prompt also:
<%= f.select(:brand, Brand.pluck(:company), {include_blank: true, class: 'form-control'}, :onchange => 'this.form.submit()') %>
Rails select helper

Cannot add styling to Select field in rails

I am using twitter-bootstrap in my rails app.
<%= f.select :rating, options_for_select([["Good","1"],["Average","2"],["Poor","3"]]), :class => "form-control" %>
But the form-control class is not working in select field
please help me to get the style working.
Thanks.
If you look at the docs, the class key is part of the last argument, which is a hash. Before that there's an options hash:
f.select :rating, options_for_select([["Good","1"],["Average","2"],["Poor","3"]]), {}, class: "form-control"
Here's an example using the options hash:
f.select :rating, options_for_select(...), { include_blank: "Choose" }, class: "foo"

Include blank and default for collection_select helper

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

How do I set a selected option in this Rails 2 selection form?

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" } %>

How to use a table column in my select box on rails?

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" } %>

Resources