正确的rails路由选择用户控制器和设计gem

时间:2015-01-25 13:07:39

标签: ruby-on-rails ruby devise

我在定制用户控制器并设计gem时遇到问题。 当我尝试到达http://localhost:3000/users

<%= link_to 'Users', users_path, class: 'navbar-brand' %>

我收到错误:

    Could not find devise mapping for path "/users". This may happen for two reasons:
1) You forgot to wrap your route inside the scope block. For example: devise_scope :user do get "/some/route" => "some_devise_controller" end 
2) You are testing a Devise controller bypassing the router. If so, you can explicitly tell Devise which mapping to use: @request.env["devise.mapping"] = Devise.mappings[:user]

如果我运行命令rake路由,我得到这个输出:

 Prefix Verb   URI Pattern                       Controller#Action
                    root GET    /                                 static_pages#home
        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
                         PATCH  /users/password(.:format)         devise/passwords#update
                         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
                         PATCH  /users(.:format)                  devise/registrations#update
                         PUT    /users(.:format)                  devise/registrations#update
                         DELETE /users(.:format)                  devise/registrations#destroy
       user_confirmation POST   /users/confirmation(.:format)     devise/confirmations#create
   new_user_confirmation GET    /users/confirmation/new(.:format) devise/confirmations#new
                         GET    /users/confirmation(.:format)     devise/confirmations#show
                   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
                         PATCH  /users/:id(.:format)              users#update
                         PUT    /users/:id(.:format)              users#update
                         DELETE /users/:id(.:format)              users#destroy
          account_update PATCH  /account_update(.:format)         users#update

这是我的路线

  root 'static_pages#home'

  devise_for :users

  resources :users
  as :user do
    patch 'account_update' => 'users#update'
    get 'users' => 'users#index'
  end

如何告诉rails / users它应该转到控制器用户并选择索引页

1 个答案:

答案 0 :(得分:3)

你可以尝试类似的东西,

devise_for :users

devise_scope :user do

end