Rails,Devise:在设计中为新创建的对象添加属性

时间:2016-10-27 12:35:25

标签: ruby-on-rails devise

这是我的user_controller,由devise生成。

   def create
     super
   end

我想,所有新用户都有#34; user"。我试过这个:

   def create
     super do
       @user.roles.create(role: "user")
     end
   end

它不起作用,但如果我在rails控制台中键入相同的代码行,它确实有效。

我需要在控制器中进行哪些更改,所有新用户都有#34;用户"?

修改

这是我的User.rb:

class User < ApplicationRecord
  after_create :add_user_role
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :directions
  has_many :roles, through: :directions



end

private
def add_user_role
  self.roles << Role.find_by_role("user")
end

创建新用户后,用户有两倍的角色:

=> #<ActiveRecord::Associations::CollectionProxy [#<Role id: 21, role: "user", created_at: "2016-10-27 15:13:44", updated_at: "2016-10-27 15:13:44">, #<Role id: 21, role: "user", created_at: "2016-10-27 15:13:44", updated_at: "2016-10-27 15:13:44">]>
irb(main):002:0> 

我有两个用户控制器,它们是:

管理员的用户控制器:

class UsersController < ApplicationController
  before_action :authenticate_user!
  before_action :current_user_allowed?



    def edit
      @user = User.find(params[:id])
    end


    def update
      @user = User.find(params[:id])

      role = params[:role]
      add_user_role(@user,role)

      redirect_to root_path

    end
end

private

def current_user_allowed?
  current_user.roles.each do |role|
    if role.role == "superadmin"
      return
    end
  end
  redirect_to root_path
end

def add_user_role(user, role1)
  user.roles.create(role: role1)
end

def user_params
  params.require(:user).permit(:role)
end

设计控制器:

class Users::RegistrationsController < Devise::RegistrationsController
# before_action :configure_sign_up_params, only: [:create]
# before_action :configure_account_update_params, only: [:update]

  # GET /resource/sign_up
  # def new
  #   super
  # end

  # POST /resource
   #def create
    # super
   #end

  # GET /resource/edit
  # def edit
  #   super
  # end

  # PUT /resource
   #def update
    # super
   #end

  # DELETE /resource
  # def destroy
  #   super
  # end

  # GET /resource/cancel
  # Forces the session data which is usually expired after sign
  # in to be expired now. This is useful if the user wants to
  # cancel oauth signing in/up in the middle of the process,
  # removing all OAuth session data.
  # def cancel
  #   super
  # end

  # protected

  # If you have extra params to permit, append them to the sanitizer.
  # def configure_sign_up_params
  #   devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute])
  # end

  # If you have extra params to permit, append them to the sanitizer.
  # def configure_account_update_params
  #   devise_parameter_sanitizer.permit(:account_update, keys: [:attribute])
  # end

  # The path used after sign up.
  # def after_sign_up_path_for(resource)
  #   super(resource)
  # end

  # The path used after sign up for inactive accounts.
  # def after_inactive_sign_up_path_for(resource)
  #   super(resource)
  # end


end

2 个答案:

答案 0 :(得分:1)

您可以使用Active Record Callbacks在用户模型中实现此逻辑,即after_create

class User << ActiveRecord::Base
  has_many :roles
  after_create :add_user_role

private
  def add_user_role
    self.roles.create(role: "user")
  end
end

答案 1 :(得分:0)

您可以像Devise wiki say那样创建一些动作,如下所示:

class User < ActiveRecord::Base
    devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :trackable, :validatable
    belongs_to :user_type

    # default usert type ('Usuario')
    before_create :default_user

    ....
    def default_user
        self.user_type_id = UserType.where(name: "Usuario").first.id
    end
end