2012-11-09 48 views
0

我有這條路線得到一個錯誤的路由從某些網頁

profile GET /contacts/:id(.:format) {:controller=>"my_devise/contacts", :action=>"profile"}

這是我的控制器/ application_controller.rb

class ApplicationController < ActionController::Base 
    protect_from_forgery 

    before_filter :get_current_user 

    def get_current_user 
     @current_user = current_user 
    end 

end 

,如果我有我的看法/佈局/應用此鏈接.html.erb文件<%= link_to "Profile", profile_path(@current_user) %>的網址http://localhost:3000/contacts/1,我沒有錯誤,但是如果我嘗試點擊網址http://localhost:3000/contacts,我得到下面的錯誤

Routing Error 

No route matches {:controller=>"my_devise/contacts", :action=>"profile"} 

如果我刪除我的application.html.erb文件中的鏈接並點擊http://localhost:3000/contacts,錯誤消失。

爲什麼該鏈接會導致此錯誤?

編輯

完全routes文件

$ rake routes 
      users_sign_out GET /users/sign_out(.:format)  {:controller=>"devise/sessions", :action=>"destroy"} 
      users_sign_in GET /users/sign_in(.:format)  {:controller=>"my_devise/sessions", :action=>"new"} 
        home GET /home(.:format)    {:action=>"index", :controller=>"my_devise/sessions"} 
       contacts GET /contacts(.:format)   {:action=>"list", :controller=>"my_devise/contacts"} 
       profile GET /contacts/:id(.:format)  {:controller=>"my_devise/contacts", :action=>"profile"} 
      edit_profile GET /contacts/:id/edit(.:format) {:controller=>"my_devise/contacts", :action=>"edit"} 
         POST /contacts/:id/edit(.:format) {:controller=>"my_devise/contacts", :action=>"update_user"} 
        more GET /more/:id(.:format)   {:controller=>"my_devise/contacts", :action=>"more"} 
         POST /home(.:format)    {:action=>"create_new_user", :controller=>"my_devise/sessions"} 
      users_sign_up GET /users/sign_up(.:format)  {:controller=>"my_devise/registrations", :action=>"new"} 
         POST /users/sign_up(.:format)  {:controller=>"my_devise/registrations", :action=>"new"} 
     new_user_session GET /users/sign_in(.:format)  {:action=>"new", :controller=>"devise/sessions"} 
      user_session POST /users/sign_in(.:format)  {:action=>"create", :controller=>"devise/sessions"} 
    destroy_user_session DELETE /users/sign_out(.:format)  {:action=>"destroy", :controller=>"devise/sessions"} 
      user_password POST /users/password(.:format)  {:action=>"create", :controller=>"devise/passwords"} 
     new_user_password GET /users/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"} 
     edit_user_password GET /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"} 
         PUT /users/password(.:format)  {:action=>"update", :controller=>"devise/passwords"} 
cancel_user_registration GET /users/cancel(.:format)  {:action=>"cancel", :controller=>"devise/registrations"} 
     user_registration POST /users(.:format)    {:action=>"create", :controller=>"devise/registrations"} 
    new_user_registration GET /users/sign_up(.:format)  {:action=>"new", :controller=>"devise/registrations"} 
    edit_user_registration GET /users/edit(.:format)   {:action=>"edit", :controller=>"devise/registrations"} 
         PUT /users(.:format)    {:action=>"update", :controller=>"devise/registrations"} 
         DELETE /users(.:format)    {:action=>"destroy", :controller=>"devise/registrations"} 
     new_user_session GET /users/sign_in(.:format)  {:action=>"new", :controller=>"my_devise/sessions"} 
         POST /users/sign_in(.:format)  {:action=>"create", :controller=>"my_devise/sessions"} 
    destroy_user_session DELETE /users/sign_out(.:format)  {:action=>"destroy", :controller=>"my_devise/sessions"} 
         POST /users/password(.:format)  {:action=>"create", :controller=>"devise/passwords"} 
         GET /users/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"} 
         GET /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"} 
         PUT /users/password(.:format)  {:action=>"update", :controller=>"devise/passwords"} 
         GET /users/cancel(.:format)  {:action=>"cancel", :controller=>"my_devise/registrations"} 
         POST /users(.:format)    {:action=>"create", :controller=>"my_devise/registrations"} 
         GET /users/sign_up(.:format)  {:action=>"new", :controller=>"my_devise/registrations"} 
         GET /users/edit(.:format)   {:action=>"edit", :controller=>"my_devise/registrations"} 
         PUT /users(.:format)    {:action=>"update", :controller=>"my_devise/registrations"} 
         DELETE /users(.:format)    {:action=>"destroy", :controller=>"my_devise/registrations"} 
       home_index GET /home/index(.:format)   {:controller=>"home", :action=>"index"} 
        root  /       {:controller=>"home", :action=>"index"} 
        root  /       {:controller=>"home", :action=>"index"} 
+0

這並沒有解決您的實際問題,但您不(不應該,無論如何)需要before filter或get_current_user方法。只需在視圖中將'@ current_user'替換爲'current_user'(與您在'get_current_user'方法中調用它相同)。 –

+0

可以顯示routes.rb – vijikumar

回答

1

爲了回答您的實際問題,那是因爲你沒有爲該路由(/contacts - 注意無ID)。你的路線是/contacts/:id(.:format) - 格式是可選的,但ID不是。您還需要使ID可選,或創建另一條路線。

+0

更新的完整路線文件的問題。我確實有一條/聯繫路線 – Catfish

+0

您的'my_devise/contacts'控制器中是否有'profile'方法? –

+0

是的。如果我從我的application.html.erb文件中刪除'<%= link_to「Profile」,profile_path(@current_user)%>'',它工作得很好。 – Catfish

0

的路由是這樣的

觸點GET /contacts(.:format){:行動=> 「清單」,:控制器=> 「my_devise /接觸」}

在這裏,我們必須給格式也。如果我們給聯繫人,它會拋出路由錯誤。所以請輸入格式。

+0

我對格式不熟悉。你能給個例子嗎? – Catfish

+0

Rails 3提供了可以在路由上指定的constraints選項。這工作根據格式路由相同的網址到不同的控制器。 – vijikumar

+0

resources:party_favors,:module =>「web」,:constraints => {:format =>:html} – vijikumar

相關問題