Reputation: 181
I have the following select drop down in my rails. I'm following the syntax from the API( http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select):
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
<%= collection_select(:sources, :source_id, Source.all, :id, :name, :include_blank => "Please select a source...", html_options = {:onchange => "updateTextArea()"} ) %>
function updateTextArea(){
alert('source changed')
}
I'm able to get the drop down to display just fine with the values from the DB when I don't include html_options. However, I'm stuck trying to get an onchange action to occur.
Upvotes: 0
Views: 578
Reputation: 171
I believe that in place of options =
or html_options =
you need to pass actual hash itself (like you actually did with include_blank => true
). I would only explicitly mark those hashes with curly braces to separate them:
<%= collection_select(:sources, :source_id, Source.all, :id, :name, { :include_blank => "Please select a source..."}, {:onchange => "updateTextArea()"} ) %>
Hope this helps.
EDIT:
I forgot to add that if updateTextArea()
JS function is not bind to a window there might be a problem with picking it up (I had similar problems in the past). For safety I would also do (if you are not using CoffeScript):
window.updateTextArea = function() { /* Your code */ }
Upvotes: 0
Reputation: 4639
I think options needs to be in a hash (the part you have include_blank
in currently). Try changing this
<%= collection_select(:sources, :source_id, Source.all, :id, :name, :include_blank => "Please select a source...", html_options = {:onchange => "updateTextArea()"} ) %>
to this
<%= collection_select(:sources, :source_id, Source.all, :id, :name, options = {include_blank: "Please select a source..."}, html_options = {:onchange => "updateTextArea()"} ) %>
Upvotes: 0