[英]Rails 3.2.9 Routes to incorrect controller
我在我的應用程序中使用了devise和cancan。 兩者都在工作。
我有一個User
模型,我剛剛添加(使用了腳手架)一個名為Purchase
的新模型。
在添加購買模型之前,我還有一個儀表板控制器(目前僅顯示一個頁面, dashboard#show
),並且是在localhost:3000/dashboard
登錄后加載的頁面。
當用戶未登錄時,我可以訪問localhost:3000/purchases
。 但是當用戶登錄后,我無法。 我可以訪問purchase/1
但不能訪問/purchases
。
知道這里發生了什么嗎? 它給我的錯誤是
“沒有路線匹配{:action =>“ show”,:controller =>“ dashboard”}“
儀表板控制器
class DashboardController < ApplicationController
def show
@user = User.find(params[:id])
authorize! :read, @user
end
end
Routes.rb
App::Application.routes.draw do
root :to => 'static_pages#index'
match '/about', :to => 'static_pages#about'
match '/error', :to => 'static_pages#error'
devise_for :users
resources :users
resources :dashboard
resource :visitors, :only => :create # POST to visitor_path to create a visitor
resources :purchases
耙道
root / static_pages#index
about /about(.:format) static_pages#about
error /error(.:format) static_pages#error
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
dashboard_index GET /dashboard(.:format) dashboard#index
POST /dashboard(.:format) dashboard#create
new_dashboard GET /dashboard/new(.:format) dashboard#new
edit_dashboard GET /dashboard/:id/edit(.:format) dashboard#edit
dashboard GET /dashboard/:id(.:format) dashboard#show
PUT /dashboard/:id(.:format) dashboard#update
DELETE /dashboard/:id(.:format) dashboard#destroy
visitors POST /visitors(.:format) visitors#create
purchases GET /purchases(.:format) purchases#index
POST /purchases(.:format) purchases#create
new_purchase GET /purchases/new(.:format) purchases#new
edit_purchase GET /purchases/:id/edit(.:format) purchases#edit
purchase GET /purchases/:id(.:format) purchases#show
PUT /purchases/:id(.:format) purchases#update
DELETE /purchases/:id(.:format) purchases#destroy
由於儀表板是單個資源,因此您需要將其設置為:
resource :dashboard
這是資源單數。
我的猜測是,在已登錄用戶的購買頁面上,您會看到諸如link_to "Dashboard", dashboard_path
,但是設置了資源后,show操作將始終需要一個id。 由於您沒有提供路由,因此會遇到路由異常。
您可以在此處找到有關單一資源的一些信息: http : //guides.rubyonrails.org/routing.html#singular-resources
單數必須是resource
,否則resources
resource :dashboard
resource :visitor, :only => :create
要么
resources :dashboards
取決於任務是什么
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.