Using onchange options in forms

Hello,

I’m trying to get a select field to update a view via the ‘onchange’
option. I’ve seen examples of it here in the forum where people hardcode
the forms and use javascript to trigger the change (and nearly all have
complained that it’s a hack) and I’m wondering if anyone has a more
elegant solution using the embedded ruby methods? I’ve tried adding the
“onchange”=>“some_action” argument to my select() but either I’m not
using it properly or it doesn’t work. Does anyone have a nice solution
using the select() method? (Or at least a way I could check if the html
option listed above is actually working?)

Thanks…
Bry M.

-----Original Message-----
I’m wondering if anyone has a more elegant solution using the
embedded ruby methods? I’ve tried adding the
“onchange”=>“some_action” argument to my select() but either
I’m not using it properly or it doesn’t work.

<%=select :item, :attribute, collection.collect {|key,value| [value,key]
},
{}, {:onChange => ‘javascript:updateView()’} %>

Will result in:


[…]

The empty hash contains options specific to the function, such as
:include_blank

Was it what you were asking for?

Nicolas Malbranche wrote:

-----Original Message-----
I’m wondering if anyone has a more elegant solution using the
embedded ruby methods? I’ve tried adding the
“onchange”=>“some_action” argument to my select() but either
I’m not using it properly or it doesn’t work.

<%=select :item, :attribute, collection.collect {|key,value| [value,key]
},
{}, {:onChange => ‘javascript:updateView()’} %>

Will result in:


[…]

The empty hash contains options specific to the function, such as
:include_blank

Was it what you were asking for?

Almost - instead of the “updateView()” javascript method I’m looking to
call a controller action.

Here’s my select code - the onChange call doesn’t seem to work, so maybe
you’ll see something I’m doing wrong.
the view:

<%= select :timespan, :span, [“1 week”, “1 month”, “3 months”, “6
months”, “1 year”], {}, {:onChange=>‘javascript:updateView()’} %>

generates the following html:

1 week 1 month 3 months 6 months 1 year

Thanks for your help

Nicolas Malbranche wrote:

Bry M. wrote:

Almost - instead of the “updateView()” javascript method I’m looking to
call a controller action.

Sorry for the delay, was out of town. OK, so as I understand it, this is
what you need:

<%= select :timespan, :span, [“1 week”, “1 month”, “3 months”, “6
months”, “1 year”], {}, {:onChange=>remote_function(:update => {
:success => “successBlock”, :failure => “errorBlock” }, :url => {
:controller => “MyController”, :action => “update_view”)} %>

This is for the code on the client side. Your page need to contain two
containers (i.e. DIVs) named successBlock and errorsBlock. On the server
side, your controller needs to have a function called update_view.

ok - I’m a little unclear on the mechanics here, though. Can you explain
how the :update args interact with the url? I understand what’s going on
in :url but I’m confused with how it works with :update. Where can I
find ‘remote_function’ defined in the source, also?

In your current code, I assume nothing happens because you don’t have a
javascript function called updateView()?

That’s correct, mainly because I’m having another problem calling
javascript inline in my view. I don’t plan on implementing things this
way if I can do it in ruby, but I’m just trying to do a little “proof of
concept” with js and not having much luck…

Thanks for your help!

Bry M. wrote:

Almost - instead of the “updateView()” javascript method I’m looking to
call a controller action.

Sorry for the delay, was out of town. OK, so as I understand it, this is
what you need:

<%= select :timespan, :span, [“1 week”, “1 month”, “3 months”, “6
months”, “1 year”], {}, {:onChange=>remote_function(:update => {
:success => “successBlock”, :failure => “errorBlock” }, :url => {
:controller => “MyController”, :action => “update_view”)} %>

This is for the code on the client side. Your page need to contain two
containers (i.e. DIVs) named successBlock and errorsBlock. On the server
side, your controller needs to have a function called update_view.

In your current code, I assume nothing happens because you don’t have a
javascript function called updateView()?

Hope that helps.