我在 Rails 3 项目中使用 MetaSearch gem。
我有两个模型:
class Company < ActiveRecord::Base
belongs_to :city
end
class City < ActiveRecord::Base
has_many :companies
end
我在 CompaniesController 中有操作:
def index
@search = Company.search(params[:search])
@companies = @search.all
end
操作的 View 包含:
= form_for @search do |f|
= f.label :city_id_equals
= f.select :city_id_equals
= f.submit 'Search'
我想要一个包含要呈现的城市名称的列表,并有机会按城市搜索公司。但是我没有城市的名称和 ID,而是类似“City:0x00000102a20488”的内容,并且搜索无法正常工作。
我认为错误在这里:“:city_id_equals”。如何使其正确?
最佳答案
解决办法找到了!
而不是:
= f.label :city_id_equals
= f.select :city_id_equals
我应该使用:
= f.label :city_id_equals
= f.collection_select :city_id_equals, City.all, :id, :name, :include_blank => true
关于ruby-on-rails - 使用 MetaSearch 在 Rails 3 中按关联进行搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6598639/