2011-07-06 44 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。 –

相關問題