简体   繁体   English

ActiveAdmin和Devise - skip_confirmation! 关于创造行动

[英]ActiveAdmin and Devise - skip_confirmation! on create action

I want to call user.skip_confirmation while his account is created by admin in admin panel. 我想调用user.skip_confirmation而他的帐户是由admin在管理面板中创建的。 I want user to confirm his account in further steps of registration process, but not on create . 我希望用户在注册过程的后续步骤中确认他的帐户,但不是在create The only idea I have is to override create in controller: 我唯一的想法是覆盖控制器中的create

controller do
  def create
    user = User.new
    user.skip_confirmation!
    user.confirmed_at = nil
    user.save!
  end
end

The problem is, I have different attr_accessible s for standard user and admin, and it works, because ActiveAdmin uses InheritedResources: 问题是,我对标准用户和管理员有不同的attr_accessible ,并且它有效,因为ActiveAdmin使用InheritedResources:

attr_accessible :name, :surname
attr_accessible :name, :surname, invitation_token, :as => :admin

It doesn't work after I changed create (it worked before). 我改变create后它不起作用(之前有效)。 How can I do what I want and still be able to use this :as => :admin feature? 我怎样才能做我想做的事情,仍然可以使用它:as => :admin feature?

I look at the answer and none is solving the issue at hand. 我看看答案,没有人解决手头的问题。 I solve it the simplest way as shown below. 我用最简单的方法解决它,如下所示。

before_create do |user|
 user.skip_confirmation!
end
controller do
  def create
    @user = User.new(params[:user].merge({:confirmed_at => nil}))
    @user.skip_confirmation!
    create! #or super
  end

  def role_given?
    true
  end

  def as_role
    # adapt this code if you need to
    { :as => current_user.role.to_sym } 
  end
end

something like that could work 类似的东西可以工作

EDIT : if you define role_given? 编辑 :如果你定义role_given? to return true and as_role , InheritResources will use as_role to get the role information 要返回true和as_roleInheritResources将使用as_role来获取角色信息

also

controller do
  with_role :admin
end

works, but this way you can't change the role given the user. 工作,但这样你就无法改变给定用户的角色。

At your /app/models/user.rb 在/app/models/user.rb

  before_create :skip_confirmation

  def skip_confirmation
    self.skip_confirmation! if Rails.env.development?
  end

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

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