2012-04-16 130 views
0

我添加了一個has_many :through協會(連接表)到我的房屋模型,因此房子可以有多重的類別,如「最後一分鐘」,「房子池」等has_many:通過關聯的網址/視圖?

我做了一個自定義查詢,因此通過例如我(5)

@lastminutes = House.find(:all, :select=>"houses.*", :joins=>"JOIN category_join_tables ON houses.id = category_join_tables.house_id", :conditions=>["category_join_tables.house_id = houses.id AND category_join_tables.category_id = ?", 5]) 

這個工作很好,在控制檯。

什麼是最好的方法來讓我的網站工作。我想url結構像這樣:

domain.com/locale/holidayhouses/lastminutes (house in the lastminute category) 
domain.com/locale/holidayhouses/holidayhouses_with_pool (houses in the pool category) 

domain.com/locale/lastminutes (houses and appartments in the lastminute category) 

任何幫助將是偉大的!

回答

1

這是很容易:

的config/routes.rb文件:

match 'search/*categories' => "controller#action" 

在你的行動,你現在得到的類別作爲一個數組,只需把它解析爲查詢。我猜你需要「搜索」,因爲否則軌道將把每一個正常的請求作爲類別並將你重定向到動作。

看到How to embed multiple tags in Rails routes, like Stack Overflow


對於l18n部分,你可以不喜歡它

scope "/:locale" do 
    # move all the other routes in here to work with the locale 
end 

檢查出rails guide上國際化