views:

2747

answers:

2

how do i add onchange event here?
Framework: rails
Database: MySQL
am populating the options from the database and tat made me to use options_from_collection_for_select
select_tag(:variable,options_from_collection_for_select(:all, :id, :name))
thanks in advance.

+1  A: 

select_tag takes an options hash as its final parameter, in which you can add any HTML attributes for the select. So to add an onchange attribute:

select_tag :variable, options_from_collection_for_select(:all, :id, :name), :onchange => 'your_onchange_handler()'
Daniel Vandersluis
will i be able to specify an action in a controller as an onchange event? if not, how do i do it then?
Nave
I'm not quite sure what you mean?
Daniel Vandersluis
i have an action in my controller to be executed and not a javascript event hander. will i be able able to do it with onchange event or are there any other ways?
Nave
DOM events can only fire javascript event handlers. You could have your event handler make an AJAX call to the action you want executed, though.
Daniel Vandersluis
can i get some code example for that?
Nave
I don't know exactly what you want to do of course, but take a look at the observe_field (http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M001622) method -- you can use that instead of the above onchange handler to set up a Rails action to be called when an input is changed. Hopefully that will put you in the right direction!
Daniel Vandersluis
+1  A: 

try something like:

:onchange => remote_function(:url => {:controller => 'controller', :action => 'action'})
marsjo