2013-05-21 27 views
2

我想調用user.skip_confirmation而他的帳戶是由admin在管理面板中創建的。我希望用戶在註冊過程的進一步步驟中確認他的帳戶,但不是create。我唯一的想法是在控制器覆蓋createActiveAdmin和設計 - skip_confirmation!在創建行動

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

的問題是,我有不同的attr_accessible S代表標準用戶和管理員,和它的作品,因爲ActiveAdmin使用InheritedResources:

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

在我更改了create之後(之前有效),它不起作用。我怎樣才能做到我想要的功能,仍然能夠使用這個功能?

回答

0
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 

的作品,但這種方式你不能改變給予用戶的角色。

+0

不,在這種情況下':as =>:admin'不起作用。我得到了'不能批量分配受保護的屬性' – ciembor

+0

嗯......這就是我之前做的,它的工作原理,除非我重寫'create'。 – ciembor

+0

你在叫'超級'嗎? – Orlando

0

在你/app/models/user.rb

before_create :skip_confirmation 

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

我看答案,完全沒有手解決問題。我解決它是最簡單的方式,如下所示。

before_create do |user| 
user.skip_confirmation! 
end