自定义注册控制器设计不覆盖创建操作 [英] Custom Registrations Controller for Devise not Overriding Create Action

查看:106
本文介绍了自定义注册控制器设计不覆盖创建操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经和这几天战斗了。我创建了自己的注册控制器,以允许一个amdin创建和删除用户。我已经在我的设计配置中留下了:可注册的模块,因为我也希望用户能够编辑他们的配置文件。我试过把这个模块拿出来,看看能不能解决我的问题。我的问题是,当我创建一个新的用户作为管理员,它仍然签署该用户,尽管有我自己的创建动作。我已经尝试过我能想到的一切,超越这一点,我被困住了。

I have been fighting with this for days now. I have created my own Registrations Controller to allow for an amdin to create and delete users. I have left the :registerable module in my devise configuration, as I also want to users to be able to edit their profiles. I have tried taking that module out just to see if it would solve my issue. The problem that I have, is that when I create a new user as an admin, it still signs that user in, despite having my own create action. I have tried everything that I can think of to get beyond this and I am stuck.

我的注册控制员:

  class RegistrationsController < Devise::RegistrationsController

  load_and_authorize_resource

  def new
    super
  end

  def create
    resource.build
    if resource.save
      redirect_to users_path
    else
      clean_up_passwords(resource)
      render_with_scope :new
    end
  end

end

应用程序控制器:=> note, after_sign_up_path_for 被覆盖在这里作为测试,看看是否会工作

Application Controller: => note, after_sign_up_path_for was overriden here as a test to see if that would work

class ApplicationController < ActionController::Base

  protect_from_forgery

  rescue_from CanCan::AccessDenied do |exception|
    flash[:error] = exception.message
    redirect_to projects_url
  end

  protected

    def stored_location_for(resource)
      nil
    end

    def after_sign_in_path_for(resource)
      projects_url
    end

    def after_sign_up_path_for(resource)
      users_path
    end

end

路线文件:

DeviseTest::Application.routes.draw do

  devise_for :users, :controller => { :registrations => "registrations"}
  devise_scope :user do
    get '/login' => 'devise/sessions#new'
    get '/logout' => 'devise/sessions#destroy'
  end

  resources :users, :controller => "users"

  resources :projects

  root :to => 'home#index'

  end

我的用户控制器用于管理员视图

And my Users Controller for admin view

class UsersController < ApplicationController

  load_and_authorize_resource

  # GET /users
  # GET /users.xml
  def index
    @users = User.excludes( :id => current_user.id )

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @users }
    end
  end

  # DELETE /users/1
  # DELETE /users/1.xml
  def destroy
    @user = User.find(params[:id])
    @user.destroy

    respond_to do |format|
      format.html { redirect_to(users_url) }
      format.xml  { head :ok }
    end
  end
end

Rake Routes输出:

Rake Routes Output:

new_user_session GET    /users/sign_in(.:format)       {:action=>"new", :controller=>"devise/sessions"}
            user_session POST   /users/sign_in(.:format)       {:action=>"create", :controller=>"devise/sessions"}
    destroy_user_session DELETE /users/sign_out(.:format)      {:action=>"destroy", :controller=>"devise/sessions"}
           user_password POST   /users/password(.:format)      {:action=>"create", :controller=>"devise/passwords"}
       new_user_password GET    /users/password/new(.:format)  {:action=>"new", :controller=>"devise/passwords"}
      edit_user_password GET    /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
                         PUT    /users/password(.:format)      {:action=>"update", :controller=>"devise/passwords"}
cancel_user_registration GET    /users/cancel(.:format)        {:action=>"cancel", :controller=>"devise/registrations"}
       user_registration POST   /users(.:format)               {:action=>"create", :controller=>"devise/registrations"}
   new_user_registration GET    /users/sign_up(.:format)       {:action=>"new", :controller=>"devise/registrations"}
  edit_user_registration GET    /users/edit(.:format)          {:action=>"edit", :controller=>"devise/registrations"}
                         PUT    /users(.:format)               {:action=>"update", :controller=>"devise/registrations"}
                         DELETE /users(.:format)               {:action=>"destroy", :controller=>"devise/registrations"}
                   login GET    /login(.:format)               {:controller=>"devise/sessions", :action=>"new"}
                  logout GET    /logout(.:format)              {:controller=>"devise/sessions", :action=>"destroy"}
                   users GET    /users(.:format)               {:action=>"index", :controller=>"users"}
                         POST   /users(.:format)               {:action=>"create", :controller=>"users"}
                new_user GET    /users/new(.:format)           {:action=>"new", :controller=>"users"}
               edit_user GET    /users/:id/edit(.:format)      {:action=>"edit", :controller=>"users"}
                    user GET    /users/:id(.:format)           {:action=>"show", :controller=>"users"}
                         PUT    /users/:id(.:format)           {:action=>"update", :controller=>"users"}
                         DELETE /users/:id(.:format)           {:action=>"destroy", :controller=>"users"}
                projects GET    /projects(.:format)            {:action=>"index", :controller=>"projects"}
                         POST   /projects(.:format)            {:action=>"create", :controller=>"projects"}
             new_project GET    /projects/new(.:format)        {:action=>"new", :controller=>"projects"}
            edit_project GET    /projects/:id/edit(.:format)   {:action=>"edit", :controller=>"projects"}
                 project GET    /projects/:id(.:format)        {:action=>"show", :controller=>"projects"}
                         PUT    /projects/:id(.:format)        {:action=>"update", :controller=>"projects"}
                         DELETE /projects/:id(.:format)        {:action=>"destroy", :controller=>"projects"}
                    root        /(.:format)                    {:controller=>"home", :action=>"index"}

其他一切都按预期工作,我无法让创建的用户无法登录。这不是一个巨大的问题一个用户,但如果我需要创建3或4,这是一个巨大的皮塔不得不注销,登录,每一次。

Everything else works as expected, I just cannot get the created user to not be signed in. It's not a huge issue for creating one user, but if I need to create 3 or 4, it's a huge p.i.t.a to have to signout, signin, every single time.

任何帮助在这是非常大的感谢。

Any help on this is greatly appreciated.

推荐答案

在route.rb文件的第三行,我想你的意思是:controllers => ... ,而不是:controller => ...... 。你错过了's'。

On the third line of your routes.rb file, I think you mean :controllers => …, not :controller => …. You're missing an 's'.

这篇关于自定义注册控制器设计不覆盖创建操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆