Scott
2007-Feb-22 01:36 UTC
How do I get the selected value from a collection_select in the view for an Ajax.Updater call?
I have the classic issue where I have two select boxes, the second''s
contents dependent on the first. I found some same code that works
fairly well using an Ajax.Updater. However, I need to pass in the
selected value of the original select list to the ajax call. I am
trying to use this[this.selectedIndex].value but keep getting a
"undefined local variable or method `this'' for
#<#<Class:0x3143860>:
0x3143838>".
Any ideas? Here''s a my snippet of code from my view.
collection_select(''product'',
''departments'',
@departments,
:id, :name,
{:prompt => "Select"},
html_options {
:onChange => "new
Ajax.Updater(''categories_span'',
''/mgs/
department_changed/''" + this[this.selectedIndex].value + ",
{asynchronous:true,
evalScripts:true});"
})
%>
Thanks,
Scott
--~--~---------~--~----~------------~-------~--~----~
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-/JYPxA39Uh5TLH3MbocFFw@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
-~----------~----~----~----~------~----~------~--~---
François Beausoleil
2007-Feb-22 02:11 UTC
Re: How do I get the selected value from a collection_select in the view for an Ajax.Updater call?
Hello Scott, 2007/2/21, Scott <tamosunas@gmail.com>:> Any ideas? Here's a my snippet of code from my view.You're falling into the classic hole of Ruby vs JavaScript code.> collection_select('product', > 'departments', > @departments, > :id, :name, > {:prompt => "Select"}, > html_options > { > :onChange => "new > Ajax.Updater('categories_span', > '/mgs/ > department_changed/'" + this[this.selectedIndex].value + ", > {asynchronous:true, > evalScripts:true});" > }) > %>Your onChange parameter must be a string that will be evaluated client-side. The way your wrote it, you are calling this on the Rails View object, which is the error you are getting. You must either: escape your quotes, or use the %Q() operator to enable you to use quotes inside a string, without escaping: :onChange => %Q( new Ajax.Updater('categories_span', '/mgs/department_changed/' + this[this.selectedIndex].value, {asynchronous:true, evalScripts:true) Hope that helps ! -- François Beausoleil http://blog.teksol.info/ http://piston.rubyforge.org/ --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Scott
2007-Feb-22 02:56 UTC
Re: How do I get the selected value from a collection_select in the view for an Ajax.Updater call?
Hi François,
Thanks for getting back to me so quickly.
That sort of worked. When I wrap in the %Q, I get the following in the
source of the rendered page:
<select id="product_departments"
name="product[departments]"
onChange="new Ajax.Updater(''categories_span'',
''/mgs/
department_changed/'' + this[this.selectedIndex].value + ,
{asynchronous:true,
evalScripts:true});">
Unfortunately, the controller is called when the item''s selection is
changed.
If I subsitute the this[this.selectedIndex].value with 4 for example,
just to see if it works, it does in fact update the second select box.
Any other ideas?
Thanks again,
Scott
On Feb 21, 9:11 pm, "François Beausoleil"
<francois.beausol...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Hello Scott,
>
> 2007/2/21, Scott <tamosu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:
>
> > Any ideas? Here''s a my snippet of code from my view.
>
> You''re falling into the classic hole of Ruby vs JavaScript code.
>
> > collection_select(''product'',
> > ''departments'',
> > @departments,
> > :id, :name,
> > {:prompt => "Select"},
> > html_options > >
{
> > :onChange => "new
> > Ajax.Updater(''categories_span'',
> > ''/mgs/
> > department_changed/''" + this[this.selectedIndex].value +
",
> > {asynchronous:true,
> > evalScripts:true});"
> > })
> > %>
>
> Your onChange parameter must be a string that will be evaluated
> client-side. The way your wrote it, you are calling this on the Rails
> View object, which is the error you are getting.
>
> You must either: escape your quotes, or use the %Q() operator to
> enable you to use quotes inside a string, without escaping:
>
> :onChange => %Q(
> new Ajax.Updater(''categories_span'',
''/mgs/department_changed/'' +
> this[this.selectedIndex].value, {asynchronous:true, evalScripts:true)
>
> Hope that helps !
> --
> François Beausoleilhttp://blog.teksol.info/http://piston.rubyforge.org/
--~--~---------~--~----~------------~-------~--~----~
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---