I have just read the Rails API definition for option_from_collection_for_select.
Returns a string of option tags that have been compiled by iterating over the collection and assigning the result of a call to the value_method as the option value and the text_method as the option text
Now I am very new to rails so was wondering if someone could break this down into smaller chunks and explain what is happening, dumb it down if you will, the explanation is very high level (well for me at the moment)
Thank you
Using the example from the Ruby on Rails API, let’s assume you have a
Personmodel that has anidattribute and anameattribute.Say you have a form that creates a new
projectand assigns it to aperson. Maybe you want to have a drop down select for whatpersonyou’re assigning this project to. You could useoptions_from_collection_for_selectfor something like this.(
fby the way would be referring to the@projectvariable for our example form here.)What this would do is create an option in your select drop-down for each
personcontained in the instance variable@people. Each of the<option>tags would have theidof thatpersonassigned to itsvalueattribute, and the text for that option would be theperson‘sname.So if your
@peoplevariable contained[#<Person id: 1, name: "Brock Sampson">, #<Person id: 2, name: "Byron Orpheus">], you would get HTML output like this:Does that make more sense?