如何在站点和管理页面中使用Devise

时间:2015-04-17 10:42:16

标签: ruby-on-rails ruby ruby-on-rails-4 devise routes

我是RoR和Devise的新手,我在设计用户身份验证方面陷入困境。我正在开发一个网站,其中包含管理页面。我的结构看起来像这样:

app
 |-controllers
       |-admin
           |- user_list_controller.rb //Every user crud operations are in it.

 |-views
     |-admin
         |-user_list
             |-new.html.erb
             |-edit.html.erb
         |-devise //Also have devise views

UserListController:

class Admin::UserListController < ApplicationController
    layout 'admin/admin'
  def index
    @user_list = User.all
  end

  def new
    @user = User.new
  end

  def edit
  end

  def delete
  end

  def create_user

  end
end

我想要做的是,我想在这个控制器下使用Devise方法,但这是我坚持的地方。我创建了一个UserController,其基础是Devise :: RegistrationsController。但是这次我收到了这个错误,

  

找不到路径&#34; / admin / create_user&#34;的设计映射。这可能   发生的原因有两个:1)你忘了把你的路线包裹在里面   范围块。例如:devise_scope:user do get&#34; / some / route&#34; =&GT;   &#34; some_devise_controller&#34;结束2)您正在测试Devise控制器   绕过路由器。如果是这样,你可以明确地告诉Devise   映射使用:@ request.env [&#34; devise.mapping&#34;] =   Devise.mappings [:用户]

看起来像route.rb错误,我的路径文件是:

Rails.application.routes.draw do


  devise_for :users
  devise_scope :user do 
    # post "admin/add_user" =>"admin/user_list#create_user", as: :adduser
  end

   namespace :admin do
    root to: 'admin#index'
    get 'user_list', :to => 'user_list#index'
    post 'create_user', :to => "user#create"
    get 'new_user', :to => 'user_list#new'

    get 'user_proposals', :to => 'user_proposal_forms#index'
    get 'user_appointments', :to => 'user_appointments#index'
    get 'brands', :to => 'brands#index'
    get 'brand_makes', :to => 'brand_makes#index'
    get 'make_types', :to => 'make_types#index'
  end

end

rake路线的结果如下:

Prefix Verb             URI Pattern                 Controller#Action 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
        admin_root GET    /admin(.:format)                   admin/admin#index
   admin_user_list GET    /admin/user_list(.:format) admin/user_list#index
admin_create_user POST   /admin/create_user(.:format)       admin/user#create
    admin_new_user GET    /admin/new_user(.:format)          admin/user_list#new
admin_user_proposalsGET /admin/user_proposals(.:format)    admin/user_proposal_forms#index
admin_user_appointments GET /admin/user_appointments(.:format) admin/user_appointments#index
admin_brands GET    /admin/brands(.:format)            admin/brands#index
admin_brand_makes GET  /admin/brand_makes(.:format)    admin/brand_makes#index
 admin_make_types GET    /admin/make_types(.:format)   admin/make_types#index

看起来很乱,对不起。最后我的form_for看起来像这样:

<%= simple_form_for @user, url: admin_create_user_path, class: "form-horizontal" do |f| %>

     <%= render(:partial => "form", :locals => {:f => f}) %>

 <% end %>

那我在哪里弄错了?我已经阅读了Devise中的所有文档,尝试了很多东西,但无法解决问题。

1 个答案:

答案 0 :(得分:1)

在路线档案中使用

 devise_for :users, :controllers => { registrations: 'registrations' }