注册后如何创建新对象并将其分配给用户?

时间:2014-12-12 15:36:33

标签: ruby-on-rails devise

如何在使用设计注册后为用户创建和分配新对象?

我创建了一个配置文件模型来保存用户模型的属性,例如" name" "位置" "描述" "相片"等。

我使用Devise注册User模型的用户,该模型只保存电子邮件和密码。

我想将这些属性从一个用户模型中分离出来,这样就可以在没有密码的情况下更新用户配置文件属性。

我尝试过after_create回调来初始化一个新的个人资料对象,但它没有用。

"未定义的局部变量或方法`current_user'对于#"

User.rb

class User < ActiveRecord::Base      
  devise :database_authenticatable, :registerable,
             :recoverable, :rememberable, :trackable, :validatable
  has_one :profile, dependent: :destroy
  after_create :setup_profile

  protected

  def setup_profile
    @profile = current_user.profile.create
  end
end

my_devise / registrations_controller.rb

class MyDevise::RegistrationsController < Devise::RegistrationsController
  def create
    build_resource(registration_params)

    if resource.save
      if resource.active_for_authentication?
        set_flash_message :notice, :signed_up if is_navigational_format?
        sign_up(resource_name, resource)
        respond_with resource, :location => after_sign_up_path_for(resource)
      else
        set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
        respond_with resource, :location => after_sign_up_path_for(resource)
      end
    else
      #clean_up_passwords
      respond_with resource
    end
  end 

  def update
    @user = User.find(current_user.id)

    successfully_updated = if needs_password?(@user, params)
      @user.update_with_password(devise_parameter_sanitizer.sanitize(:account_update))
    else
      # remove the virtual current_password attribute
      # update_without_password doesn't know how to ignore it
      params[:user].delete(:current_password)
      @user.update_without_password(devise_parameter_sanitizer.sanitize(:account_update))
    end

    if successfully_updated
      set_flash_message :notice, :updated
      # Sign in the user bypassing validation in case their password changed
      sign_in @user, :bypass => true
      redirect_to after_update_path_for(@user)
    else
      render "edit"
    end
  end

  private

  # check if we need password to update user data
  # ie if password or email was changed
  # extend this as needed

  def needs_password?(user, params)
    user.email != params[:user][:email] ||
      params[:user][:password].present? ||
      params[:user][:password_confirmation].present?
  end
    # https://github.com/plataformatec/devise/wiki/How-To%3a-Allow-users-to-edit-their-account-without-providing-a-password
  def needs_pass?(user, params)
    params[:password].present?
  end

  def registration_params
    params.require(:user).permit(:email, :username, :password, :password_confirmation)
  end

  def user_params
      params.require(:user).permit(:name, :username, :location, :description, :website)
  end

  protected 
  def after_update_path_for(resource)
    edit_user_registration_path(resource)
  end

  def after_sign_up_path_for(resource)
    current_user
  end

end

1 个答案:

答案 0 :(得分:2)

current user在模型中不可用。当您使用回调时,您实际上指的是您使用的对象(注册的用户)。

  after_create :setup_profile

  def setup_profile
    profile = self.create_profile # or Profile.create(user_id: self.id)
  end