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:

HTML <select> selected option background-color CSS style

其他提示

For the CSS related part of this question I'm referencing to answer on the link below:

https://stackoverflow.com/a/1947675

In short, you need to add empty hash {} in order for it to work. So it will be like:

<%= collection_select(:user, :state_id, State.order('name'), :id, :name, {}, {:class=>'collection-select-class'})  %>

Hope this helps!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top