2012-07-03 132 views
10

所以我一直運行到以下錯誤:導軌路由錯誤? 「沒有路由匹配」

No route matches {:action=>"show", :controller=>"users"}

我試圖運行耙路線,我可以看到路由存在:

user   GET   /users/:id(.:format)  users#show 
       PUT   /users/:id(.:format)  users#update 
       DELETE  /users/:id(.:format)  users#destroy 

但每次我嘗試訪問頁面"https://stackoverflow.com/users/1"(1是用戶ID),我得到上述錯誤。有任何想法嗎?謝謝!

這裏是我的routes.rb

SampleApp::Application.routes.draw do 
    root to: 'static_pages#home' 

    resources :users 
    resource :sessions, only: [:new, :create, :destroy] 


    match '/signup', to: 'users#new' 
    match '/signin', to: 'sessions#new' 
    match '/signout', to: 'sessions#destroy', via: :delete 

    match '/help', to: 'static_pages#help' 
    match '/about', to: 'static_pages#about' 
    match '/contact', to: 'static_pages#contact' 

這裏是我的users_controller.rb

class UsersController < ApplicationController 

    before_filter :signed_in_user, only: [:index, :edit, :update] 
    before_filter :correct_user, only: [:edit, :update] 

    def show 
    @user = User.find(params[:id]) 
    end 

    def new 
    @user = User.new 
    end 

    def create 
    @user = User.new(params[:user]) 
    if @user.save 
     sign_in @user 
     flash[:success] = "Welcome to the Paper Piazza!" 
     redirect_to @user 
    else 
     render 'new' 
    end 
    end 

    def edit 
    @user = User.find(params[:id]) 
    end 

    def update 
    if @user.update_attributes(params[:user]) 
     flash[:success] = "Profile updated" 
     sign_in @user 
     redirect_to @user 
    else 
     render 'edit' 
    end 
    end 

    def index 
    @users = User.paginate(page: params[:page]) 
    end 

    private 

    def signed_in_user 
     unless signed_in? 
     store_location 
     redirect_to signin_path, notice: "Please sign in." 
     end 
    end 

    def correct_user 
     @user = User.find(params[:id]) 
     redirect_to(root_path) unless current_user?(@user) 
    end 
end 
+0

顯示您的routes.rb文件 –

+0

是否'/用戶/ 1 /'工作? – sarnold

+0

不,'/ users/1 /'似乎不起作用。 – user1497531

回答

1

嘗試固定錯字:

root :to 'static_pages#home' 

(而不是root to:),並將其移動到最後塊的行。讓我知道這是否有所作爲!

有什麼奇怪的是,我建立與簡單的讀取路由文件中的新項目:

RoutingTest::Application.routes.draw do 
    resources :users 
    root :to => "static_pages#home" 
end 

當我跑這在控制檯中,我得到了你所看到的同樣的錯誤:

>> r = Rails.application.routes ; true 
=> true 
>> r.recognize_path("https://stackoverflow.com/users/1") 
=> ActionController::RoutingError: No route matches "/users" 

...但是當我在一個稍微大一點的項目運行同樣的事情,我得到:

>> r = Rails.application.routes ; true 
=> true 
>> r.recognize_path("https://stackoverflow.com/users/1") 
=> {:action=>"show", :controller=>"users", :id=>"1"} 

所以我沒有什麼confid因爲我告訴你的事情會有所作爲。 (除此之外,Rails.application.routes技巧對驗證控制檯中的路徑很有用!)

+0

注意保留名稱。儘量不要使用static_pages,看看你得到了什麼。 – bcackerman

0

我有一個類似的問題,我有路線只會返回根頁面,但會顯示在rake routes。根據開發人員的具體命名空間和所有路線root to: (routing)必須始終是最後一個。這與Rails路由指南相矛盾(請參閱http://guides.rubyonrails.org/routing.html#using-root),其中說任何對root的調用都應該位於文件的頂部。這絕對不適合我。有趣的...

相關問題