【问题标题】:Rails 3.2.9 Routes to incorrect controllerRails 3.2.9 路由到不正确的控制器
【发布时间】:2013-01-11 06:18:01
【问题描述】:

我在我的应用程序中使用了 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

【问题讨论】:

  • 好像你的dashboard和你的购买没有关系,是不是在cancan中处理能力有问题?此外,如果您想限制其他人的仪表板,最好使用 before_filter :authenticate_user!也一样?
  • 向我们展示您的rake routes
  • 我在顶部进行了编辑以显示 rake 路线。 @Nich cancan 可以与我的其他页面一起使用,我还没有尝试在购买时使用。

标签: ruby-on-rails controller routes


【解决方案1】:

单数必须为resource,否则为resources

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

resources :dashboards

取决于任务是什么

【讨论】:

    【解决方案2】:

    由于仪表板是单一资源,您需要将其设置为:

    resource :dashboard
    

    这是资源单数。

    我的猜测是,在登录用户的购买页面上,您有类似link_to "Dashboard", dashboard_path 的内容,但是使用资源设置时,显示操作将始终需要一个 id。由于您没有提供,因此您会遇到路由异常。

    你可以在这里找到一些关于奇异资源的信息:http://guides.rubyonrails.org/routing.html#singular-resources

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多