2016-05-12 98 views
0

我試圖通過Devise實現邪惡的寶石,因爲我希望用戶通過不同的步驟來完成他們的配置文件。我是一個全新的新手,所以如果你能給我一個關於可能是什麼問題的建議,我將不勝感激。未定義的方法`屬性'爲零:NilClass - 邪惡/設計寶石

我得到的錯誤是這一個,它顯示當我嘗試從「個人」繼續「樣式」的一步。我想這是與保存數據的問題:

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 
+0

錯誤告訴您current_user在啓動控制器的更新操作中不存在。 –

+0

嗯,但它確實存在,因爲你可以看到... –

+1

不,我們不能看到。這可能是用戶根本沒有登錄。 – Leito

回答

1

隨着設計,current_usernil如果沒有用戶登錄。因此,您的問題是,您正在爲您的update操作分配@user = current_user而未驗證用戶已登錄。

如果你想確保update動作只在用戶簽名,則使用由設計所提供的authenticate_user!助手作用:

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

    # ... 
end 

authenticate_user! helper方法將用戶重定向到登錄如果頁面他們沒有登錄。如果用戶成功登錄,current_user將被設置,他們將被重定向回原來試圖訪問的頁面。

+0

這是因爲身份驗證謝謝!但是現在我在同一行發現了一個新錯誤:ActiveModel :: ForbiddenAttributesError –

+0

您需要將使用強參數從表單傳遞的參數列入白名單:http://edgeguides.rubyonrails.org/action_controller_overview.html #強參數。 –

+0

非常感謝安東尼!它正在工作! :) –

相關問題