我有以下内容:
<%= collection_select 'product', 'id', @products, 'id', :description %>
- 与前面的关键字搜索匹配的产品列表。
我想使用更具体的描述,它是由模型的方法构建的specific_description(keyword)
。尽管模型无权访问关键字(params [:q])。使用collection_select时,可以使用什么将参数传递给text_method(在我的例子中是描述)?
我有以下内容:
<%= collection_select 'product', 'id', @products, 'id', :description %>
- 与前面的关键字搜索匹配的产品列表。
我想使用更具体的描述,它是由模型的方法构建的specific_description(keyword)
。尽管模型无权访问关键字(params [:q])。使用collection_select时,可以使用什么将参数传递给text_method(在我的例子中是描述)?
添加attr_accessor :keyword
到Product
模型并在里面使用它specific_description
:
class Product < ActiveRecord::Base
attr_accessor :keyword
def specific_description
# use @keyword here
end
end
在控制器中:
@products.each { |product| product.keyword = params[:q] }
现在您可以collection_select
在不传递参数的情况下调用specific_description
:
<%= collection_select 'product', 'id', @products, 'id', :specific_description %>