Leonel . wrote in post #1018324:
I have an edit form and I need to get the SELECTED VALUE from a
collection_select in a variable.
So if I have this…
<%= f.collection_select :part_id, @parts, :id, :cost_and_name, :prompt
=> “Select” %>
How can I peek inside and get, for example, the part_id?
Okay, look at this:
| | | |
| browser| 2) | server |
| | <— response – | |
±-------+ (html page) ------------
3) User makes 1) ruby code
a selection executes
What you are asking is: “In step 1, can I peek and get the user’s
selection?” The answer, obviously, is no. The ruby code executes
before the user makes a selection.
However, if your select is inside a form, then when the user submits the
form, the selection will be in params[], and it can be accessed like
this: params[:name_of_select]. If you don’t know what the value of the
select’s name attribute is, do a ‘View Source’ when the select appears
in your browser, and look at the name attribute.
You can also use javascript to get the value of the select, but you need
to know javascript/jquery to do that, and based on your question, I’m
guessing you don’t know either.
Or, are you asking what choice will be pre-selected? The docs say this:
collection_select(object, method, collection, value_method, text_method,
options = {}, html_options = {})
Returns and tags for the collection of existing return
values of method for object‘s class. The value returned from calling
method on the instance object will be selected.