I have been checking a lot of posts here in Stack Overflow, Rails documentation from http://api.rubyonrails.org/ and other places. I am absolutely confused as to how many parameters I should pass. I have seen 4 and 5. This is what I want to do.
I have a form that has a country name that I want to change to a drop down list using another table with a list of countries.
The current code on my form is:
<%= f.text_field :country %>
Country is on another table User. Instead of having people type in the country I want them to select the country from table Country listing name instead of id from the table, then use name from table Country to update country on table User.
I am still learning the current database terminology. I am used to using database, tables within a database, records/row within a table, field.column on a record, etc.
How would I code the collection_select statement to produce the list of country names from another table?
I have spent hours almost all day long trying to figure this out. Somehow I was able to figure out how to use seeds.rb to load my Country table.
I am using Rails 3.2.3 and Ruby 1.9.3. I am using a PostgreSQL database.
Any help would be appreciated.
Depending if the proper relationships to the other table has been set “such as belongs_to or has_many” you could do something like this.
Should render out as
You should read this page http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select
The first argument is the instance of the class (“Active Record / table”) to be used for the select tag your form is for.
The second is a method call to get the attribute of object corresponding to the select tag such as how country belongs_to your main active record ie “country_id / contry_ids”
The third argument is an array of objects representing the tags. Basicly all your contries ie.. “Country.all”
The fourth is the name of a method which, when called on a member of collection, returns an array of child objects representing the tags. Basically the id of the the country instance found with Country.all which is be put in the html value=”some country id”
The next argument is how you display the name of the country.
The next after that is a set of options which I’m not going to go over.