2

我有以下内容:

<%= collection_select 'product', 'id', @products, 'id', :description %>

- 与前面的关键字搜索匹配的产品列表。

我想使用更具体的描述,它是由模型的方法构建的specific_description(keyword)。尽管模型无权访问关键字(params [:q])。使用collection_select时,可以使用什么将参数传递给text_method(在我的例子中是描述)?

4

1 回答 1

1

添加attr_accessor :keywordProduct模型并在里面使用它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 %>
于 2015-12-28T17:56:59.383 回答