<%= collection_select(:catgory, :id, @categories, :id, :title, {}, data: { behavior: 'category_dropdown' }) %>
在上面的代码中,我需要将一个参数传递给 title 方法。有什么办法可以用 collection_select 做到这一点吗?
<%= collection_select(:catgory, :id, @categories, :id, (:title, @program), {}, data: { behavior: 'category_dropdown' }) %>
编辑: 查看 collection_select text_method 的内部结构。它最终被传递给 .send 方法,该方法应该允许 element.send(:title, @program)。但是,我认为我仍然无法传递参数的问题是集合选择将 (:title, @program) 读取为两个参数而不是一个。
最佳答案
改用select
:
select "catgory", "id", @categories.map{|c| [c.title(@program), c.id]}, {}, data: { behavior: 'category_dropdown' }
应该有效。
关于ruby-on-rails - 将参数传递给 text_method 的集合选择方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11781927/