Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have the next code to display my countries table in a select box:


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

and i want to set an 'onchange' action when a country es selected... I've tried:


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

like image 365
Mr_Nizzle Avatar asked Dec 19 '10 18:12

Mr_Nizzle


1 Answers

Hi format of this helper is

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

so try

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

and also check html output of your version

Formbuilder#select

like image 101
Bohdan Avatar answered Oct 20 '22 20:10

Bohdan