How to set html_options for the 'select' helper in Ruby on Rails?

I have the following code to display the table of my countries in the selection box:

f.select("country_id", Country.all.collect {|p| [ p.name, p.id ] }, {:include_blank => 'Select a Country'}) 

and I want to set the action "onchange" when the country es is selected ... I tried:

 f.select("country_id", Country.all.collect {|p| [ p.name, p.id ] }, {:include_blank => 'Select a Country',:onchange=>"alert('foo')"})
f.select("country_id", Country.all.collect {|p| [ p.name, p.id ] }, {:include_blank => 'Select a Country',:onchange=>"alert('foo')"}) 

but nothing happens ....

any help on this?

Thanks. Mr Nizzle

+4
source share
1 answer

Hi format of this helper

 select(object, method, choices, options = {}, html_options = {}) 

try

 f.select("country_id", Country.all.collect {|p| [ p.name, p.id ] }, {:include_blank => 'Select a Country'},{:onchange=>"alert('foo')"}) 

and also check the html output of your version

+14
source

Source: https://habr.com/ru/post/1332684/


All Articles