2011-07-06 41 views
2

我在我的Rails 3项目中使用MetaSearch gem。通过关联在Rails 3中使用MetaSearch搜索

我有两个型号:

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 

动作的视图包含:

= form_for @search do |f| 
    = f.label :city_id_equals 
    = f.select :city_id_equals 
    = f.submit 'Search' 

我想与城市名称的列表被渲染和有机会按城市搜索公司。但不是城市的名称和ID我有像“城市:0x00000102a20488”和搜索无法正常工作。

我认为错误在这里:“:city_id_equals”。如何使其正确?

回答

5

找到了解决方案!

相反的:

= 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 
0

不确定你的问题是否真的很清楚。

首先,我猜你有类似<City:0x00000102a20488>的东西,这是一个红宝石对象的字符串表示形式。如果你想显示城市的名字,city.name应该诀窍(假设你在城市有一个名字成员!)。

对于搜索,我真的不明白你想要做什么。 :city_id_equals应该是什么意思?

+0

正如我难过,我想搜索属于所选城市的企业。在这里阅读MetaSearch gem的文档:[链接](http://metautonomo.us/projects/metasearch/),并且有关联字段的例子:“f.text_field:developers_notes_note_contains”。我想要这样的东西,但有一个选择标签,而不是text_field。 –

相关问题