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? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 obtain 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 one. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
It''s a form. Has a drop-down menu, created by a collection_select. Since it''s an edit page, then the value is ALREADY SELECTED by the time you get to that edit page. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Walter Lee Davis
2011-Aug-25 14:12 UTC
Re: Re: Get selected value from a collection_select
That default value is set by the model, through the controller, when
the view is rendered.
At what level do you need to *get* the value? (Because, as 7stud
pointed out, it''s already known to the system.) If you need to get the
value in a JavaScript within the page, you can either use rjs to set
the variable value, or you can use regular JS to read the current
value out of the form element.
val = $F(''your_element_id''); //prototype JS
Walter
On Aug 25, 2011, at 9:48 AM, Leonel *.* wrote:
> It''s a form. Has a drop-down menu, created by a collection_select.
> Since
> it''s an edit page, then the value is ALREADY SELECTED by the time
you
> get to that edit page.
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonrails-
> talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
> .
> For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
> .
>
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
On Thu, Aug 25, 2011 at 5:26 AM, Leonel *.* <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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? >the value selected when the page is rendered will be equal to f.object.part_id> > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.com -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.