简体   繁体   English

Rails 3.2.9路由到错误的控制器

[英]Rails 3.2.9 Routes to incorrect controller

I am using devise and cancan in my app. 我在我的应用程序中使用了devise和cancan。 Both are working. 两者都在工作。

I have a User model and I have just added (using scaffolding) a new model called Purchase . 我有一个User模型,我刚刚添加(使用了脚手架)一个名为Purchase的新模型。

Before the Purchase model was added i also have a dashboard controller (which just shows a single page at the moment, dashboard#show ) and is the page that gets loaded after login at localhost:3000/dashboard 在添加购买模型之前,我还有一个仪表板控制器(目前仅显示一个页面, dashboard#show ),并且是在localhost:3000/dashboard登录后加载的页面。

When a user is not logged in, i can access localhost:3000/purchases . 当用户未登录时,我可以访问localhost:3000/purchases But when the user is logged in I cannot. 但是当用户登录后,我无法。 I can access purchase/1 but not /purchases . 我可以访问purchase/1但不能访问/purchases

Any idea what is going on here? 知道这里发生了什么吗? The error it gives me is 它给我的错误是

"No route matches {:action=>"show", :controller=>"dashboard"}" “没有路线匹配{:action =>“ show”,:controller =>“ dashboard”}“

Dashboard Controller 仪表板控制器

class DashboardController < ApplicationController
    def show
        @user = User.find(params[:id])
        authorize! :read, @user
    end

end

Routes.rb 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

Rake Route 耙道

                    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

Since Dashboard is a singular resource you need to set it up as: 由于仪表板是单个资源,因此您需要将其设置为:

resource :dashboard

that's resource singular. 这是资源单数。

My guess is that on the purchases page for logged in users, you have something like link_to "Dashboard", dashboard_path , however set up with resources, the show action will always expect an id. 我的猜测是,在已登录用户的购买页面上,您会看到诸如link_to "Dashboard", dashboard_path ,但是设置了资源后,show操作将始终需要一个id。 Since you haven't provided one, you get a routing exception. 由于您没有提供路由,因此会遇到路由异常。

You can find a bit about singular resources here: http://guides.rubyonrails.org/routing.html#singular-resources 您可以在此处找到有关单一资源的一些信息: http : //guides.rubyonrails.org/routing.html#singular-resources

Singular must be resource , else resources 单数必须是resource ,否则resources

resource :dashboard
resource :visitor, :only => :create

or 要么

resources :dashboards

depending in what the task is 取决于任务是什么

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM