Reputation: 4372
<%= collection_select(:catgory, :id, @categories, :id, :title, {}, data: { behavior: 'category_dropdown' }) %>
In the above code I need to pass a parameter to the title method. Is there any way to do this with collection_select?
<%= collection_select(:catgory, :id, @categories, :id, (:title, @program), {}, data: { behavior: 'category_dropdown' }) %>
Edit: Looking at the internals for collection_select the text_method. It is eventually passed to a .send method which should allow for element.send(:title, @program). However, I think the issue why I still can't pass the param is that collection select is reading (:title, @program) as two params instead of one.
Upvotes: 3
Views: 1863
Reputation: 1253
This can be done with collection_select
if your model has an existing parameter you can overwrite:
f.collection_select( :your_model_id,
YourModel.all.map{|ym| ym.name = ym.custom_name(your_parameter); ym},
:id, :name,
{:selected => @model_instance.logic},
{:class => 'your class', :other => "..." } )
For instance I do this to conditionally pluralize my model's name
attribute
class MyModel < ActiveRecord::Base
DEFAULT_NAME_COUNT = 99
def pluralized_name(n = DEFAULT_NAME_COUNT)
begin
return name.pluralize(n)
rescue
end
name
end
end
Upvotes: 0
Reputation: 10395
Use select
instead:
select "catgory", "id", @categories.map{|c| [c.title(@program), c.id]}, {}, data: { behavior: 'category_dropdown' }
Should be working.
Upvotes: 2