2012-03-21 51 views
0

我正在嘗試將Rails 2應用升級到Rails 3,並且我確實遇到了路由問題。下面是我在routes.rb中文件Rails 3路徑參數不是params的一部分

get 'profile/:login' => 'account#profile', :as => :profile 

當我去的http://本地主機:3000 /資料/ MyUserName輸入,它不正確添加:登錄到params哈希表。看到這裏:

Started GET "/profile/MyUsername?foo=bar" for 127.0.0.1 at Tue Mar 20 21:39:03 -0400 2012 
    Processing by AccountController#profile as HTML 
    Parameters: {"foo"=>"bar"} 

出於某種原因,:登錄不是正規參數的一部分。在一種預感,我考察了request.env,發現這個:

action_dispatch.request.path_parameters"=>{:action=>"profile", :controller=>"account", :login=>"MyUsername"} 

我在這一點上完全難住了。我錯過了什麼嗎?我應該在哪裏看看在這裏發生了什麼?

更新

我開始與消除寶石和這個神奇的工作打。我剛剛從Gemfile中註釋出了寶石,直到我獲得加載主頁所需的絕對最小集合。那時候,這些參數和預期完全一致。然後,我一次添加了幾枚寶石,以找到原因。我添加了所有內容,現在它可以工作。瘋狂,但無論如何,我想。

回答

1

看起來像是混合了'match'和'get'的語法。請嘗試:

match 'profile/:login' => 'account#profile', :as => :profile, :via => :get 

或者

get 'profile/:login', :to => 'account#profile', :as => :profile 

在你的config/routes.rb文件

+0

感謝您的建議,但我已嘗試混合並匹配'get'和'match'以及各種選項。 – Micah 2012-03-21 02:41:33

+0

你可以添加到問題的耙路線的輸出?可能你已經檢查過,但我在考慮一些路線衝突。 – jeffersongirao 2012-03-21 02:43:02

+0

我有點偏執(和尷尬)關於張貼整個路線輸出。這真的很難看,特別是考慮到它從Rails 2自動轉換爲Rails 3.我可以放心地說,唯一以「profile」開頭的路線是我想要的路線。而且,從日誌中我可以看出它正在進行正確的操作,而不是通過:login作爲params元素。 – Micah 2012-03-21 02:53:13

0

當我想使用網址參數定義我的路由時總是使用資源(S)。這是Rails 3.x的慣例,所以你可以試試它。

resources :accounts , :exclude => :all do 
    member do 
     get :profile 
    end 
    end 

這應該有助於或以任何其他方式定義資源URL。

0

像這樣的東西應該工作

match 'profile(/:login)' => "account#profile", :as => :profile 

如果不是這樣,也有可能是別的東西在你的路由文件的衝突。確保任何match ':controller(/:action(/:id(.:format)))'(或類似的「匹配所有」路線)位於路線文件的最底部。