Forum: Ruby on Rails using onchange options in forms

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bry M. (Guest)
on 2006-03-29 20:02
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.
Nicolas Malbranche (Guest)
on 2006-03-29 20:23
(Received via mailing list)
>-----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:

<select id="item_attribute" name="item[attribute]"
onChange="javascript:updateView()">
[...]
</select>

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

Was it what you were asking for?
Bry M. (Guest)
on 2006-03-29 21:29
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:
>
> <select id="item_attribute" name="item[attribute]"
> onChange="javascript:updateView()">
> [...]
> </select>
>
> 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:
<select id="timespan_span" name="timespan[span]"
onChange="javascript:updateView()">
<option value="1 week">1 week</option>
<option value="1 month">1 month</option>
<option value="3 months">3 months</option>
<option value="6 months">6 months</option>
<option value="1 year">1 year</option>
</select>

Thanks for your help
Nicolas Malbranche (Guest)
on 2006-03-30 23:41
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.
Bry M. (Guest)
on 2006-04-04 01:06
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!
This topic is locked and can not be replied to.