2011-11-16 43 views
0

我覺得我整個衝突運行,由於名稱:困惑路線設置 - 的Rails 3.1

有兩種型號:storecoupon

需要將顯示優惠券網址:http://localhost/coupons/:store_name(「消費券」,是寫在url中,不與任何代替)

控制器名稱:coupons_controller

以下是我在我的路線現在:

match '/coupons/:store_name' => 'coupons#index', :as => :stores

當我嘗試做redirect stores_path(store)在另一個控制器,我得到這個錯誤:

No route matches {:controller=>"coupons"}

任何線索?我是新手,所以我敢打賭這是一個愚蠢的錯誤。

UPDATE

是否有一個集中的地方告訴動態_path()函數中使用特定的URL結構呢?即而不必做以下隨處可見:

redirect_to stores_path(:store_name => store.store_name)

而是隻使用:

redirect_to stores_path(store)

+1

只是讓你已經修改路由文件後,重新啓動您的軌道服務器? – leflings

+0

:)我不知道它需要重新啓動 - 但現在我又遇到了另一個問題...生成的網址就像商店的ID/1234這樣。當我調用函數時,如何自動將它放入名稱中? – Jacob

回答

1

是的,你可以在你的模型重新定義to_param

class Store < ... 
    def to_param 
    store_name 
    end 
end 
1
redirect_to stores_path(:store_name => store) 

應該工作,如果它不(現在不能確定),你應該可以做(小哈克)

redirect_to stores_path+"?store_name=yourstorename" 

做它寧靜的方式,你應該有這樣的事情(在你的路線):

resources :stores do 
    resources :coupons # this will give you e.g. /stores/:store_id/coupons for the coupons#index action 
end 

如果你想使用的店鋪名稱,而不是ID,只是讓搜索使用「塞」或有一看這裏:getting a 'name' based URL in RESTful routes instead of an id based urlID + Slug name in URL in Rails (like in StackOverflow)

+0

這有效,但有沒有一種方法可以將其設置爲標準(所以可以傳入對象並告訴它在某處使用store_name而不是store_id)? – Jacob

+0

更新了我的答案 – emrass