I have the following collection_select logic in my Rails 3.2.3 form to select states and order them by name.
<%= collection_select(:user, :state_id, State.order('name'), :id, :name) %>
I have a Country model where the ID is in the State model. My User model has country_id and state_id. User belongs to Country and State. Country has many users and has many States. States belong to Country.
I would like to only select States for a particular Country. I want to match the country_id from the User model to the country_id from the State model. I thought about using State.where('something;).order('name'). However I do not know how I would code the SQL for this. I am using a PostgreSQL database. The examples I have found were for other databases.
The second question relates to how the list looks. I would like to change the background color along with the font color to match the input fields on my website. I tried the following code but could not get it working.
<%= collection_select(:user, :state_id, State.order('name'), :id, :name, {:class=>'collection-select-class'}) %>
My CSS code is:
.collection-select-class {
background-color: #333333;
color: @yellow;
}
I also tried using the option tag but it did not change anything.
Any help would be appreciated.
CSS does not currently support that feature.
Look here: