0

我正在尝试使用 Devise 实现 Wicked gem,因为我希望用户通过不同的步骤来完成他们的个人资料。我是一个完整的新手,所以如果你能给我一个关于可能是什么问题的建议,我将不胜感激。

我得到的错误就是这个错误,当我尝试从“个人”继续到“风格”步骤时它会显示出来。我想这是保存数据的问题:

NoMethodError in OnboardingController#update

undefined method `attributes' for nil:NilClass
 **@user.attributes(user_params)**

这些是我的注册和入职控制器:

class RegistrationsController < Devise::RegistrationsController


  protected 

  def after_sign_up_path_for(resource)
    '/onboarding/personal'
  end

  def after_update_path_for(resource)

    registration_steps_path

  end

  def new 

  super

  end



  def create

  super

  end



  def update 

  super

  end



  def update_resource(resource, params)
    if resource.encrypted_password.blank? # || params[:password].blank?
      resource.email = params[:email] if params[:email]
      if !params[:password].blank? && params[:password] == params[:password_confirmation]
        logger.info "Updating password"
        resource.password = params[:password]
        resource.save
      end
      if resource.valid?
        resource.update_without_password(params)
      end
    else
      resource.update_with_password(params)
    end
  end
end

class OnboardingController < ApplicationController
    include Wicked::Wizard
    steps :personal, :stylefirst


    def show
        @user = current_user
    render_wizard

    end

     def update

  @user = current_user

  @user.attributes(user_params)

  render_wizard @user

     end


end
4

1 回答 1

1

使用设计,current_user如果nil没有用户登录。所以你的问题是你在没有验证用户登录的情况下分配@user = current_user你的操作。update

如果您想确保该update操作仅对登录用户可用,请使用authenticate_user!Devise 提供的辅助操作:

class OnboardingController < ApplicationController
   before_filter :authenticate_user!, only: [:edit, :update]

   # ...
end

如果用户未登录,authenticate_user!helper 方法会将用户重定向到登录页面。如果用户成功登录,current_user将设置并将他们重定向回最初尝试访问的页面。

于 2016-05-12T15:36:57.527 回答