views:

14

answers:

1

Hi,

I am trying to add onchange handler to see which select option is currently selected and put it in a file. How can I do this?

<select id="subtable" name="subtable" size="20" style="width: 400px"> 
<%= options_from_collection_for_select(@hauses, 'id', 'timebuild', @hauses.first.id), {},  {:onchange => catch()} ) %>
</select> 

where the controller is this

def index
@hauses = Hauses.find(:all)
end

def catch
@hs = Hauses.find(params[:hauses_id])
file = File.new("catch.txt","ab")
file.puts(@hs)
file.close
end

It shows error of undefined method `catch'

Thank you for any guidance

A: 
  • A view cannot call methods of the controller, that's why he can't find the method catch.
  • You could put the method into a helper, but this would mean this method would return the Javascript code that will be filled into the onchange attribute.
  • If you'd put {:onchange => 'catch()' } this would look for the Javascript method called catch.

Onchange will be executed in the browser, so if you want to call the action "catch" when the user changes the value of the select field, then you'd have to create an AJAX call doing this. See AJAX on Rails to get you started.

Addionally, the onchange attribute has to be included in the select-tag, not the options.

giraff