【问题标题】:ActiveAdmin and Devise - skip_confirmation! on create actionActiveAdmin 和设计 - skip_confirmation!关于创建动作
【发布时间】:2013-05-21 11:14:08
【问题描述】:

我想打电话给user.skip_confirmation,而他的帐户是由管理员在管理面板中创建的。我希望用户在注册过程的进一步步骤中确认他的帐户,但不在create 上。我唯一的想法是在控制器中覆盖create

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

问题是,对于标准用户和管理员,我有不同的attr_accessibles,它可以工作,因为 ActiveAdmin 使用 InheritedResources:

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

我更改create 后它不起作用(之前它起作用)。我怎样才能做我想做的并且仍然能够使用这个:as => :admin 功能?

【问题讨论】:

    标签: ruby-on-rails devise activeadmin confirmation skip


    【解决方案1】:

    我查看了答案,但没有人能解决手头的问题。我用最简单的方法解决,如下图。

    before_create do |user|
     user.skip_confirmation!
    end
    

    【讨论】:

      【解决方案2】:
      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
      

      类似的东西可以工作

      编辑:如果你定义role_given?返回true和as_roleInheritResources将使用as_role获取角色信息

      还有

      controller do
        with_role :admin
      end
      

      有效,但这样您就无法更改给定用户的角色。

      【讨论】:

      • 不,在这种情况下:as => :admin 不起作用。我得到Can't mass-assign protected attributes
      • 嗯...这就是我之前所做的并且它有效,除非我覆盖create
      【解决方案3】:

      在你的 /app/models/user.rb

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多