2015-04-15 69 views
0

我不知道這個錯誤消息來自哪裏。我遵循Hartl的railtutorial,但適應了我自己的應用程序。我正在研究第10章,並且正在添加密碼重置。未定義的局部變量或方法`對象'的error_messages

當我得到錯誤:在服務器發送郵件後用鏈接重置密碼。然後我進入了我的瀏覽器鏈接,收到以下錯誤信息:

undefined local variable or method `object' 
Extracted source (around line #2): 
<% if object.errors.any? %> 

這指的是共享的錯誤信息文件,該文件並沒有改變之前並沒有引起問題。 我不確定發送給您什麼可能是原因。

\意見\ shared_error_messages.html.erb

​​

意見\ passwords_resets \ edit.html.erb(即下面的鏈接時,呼籲頁):

<% provide(:title, 'Reset password') %> 
<h1 class="center">Reset password</h1> 

<div class="row"> 
    <div class="col-md-6 col-md-offset-3"> 
    <%= form_for(@user, url: password_reset_path(params[:id])) do |f| %> 
     <%= render 'shared/error_messages' %> 

     <%= hidden_field_tag :email, @user.email %> 

     <%= f.label :password %> 
     <%= f.password_field :password, class: 'form-control' %> 

     <%= f.label :password_confirmation, "Confirmation" %> 
     <%= f.password_field :password_confirmation, class: 'form-control' %> 

     <%= f.submit "Update password", class: "btn btn-primary" %> 
    <% end %> 
    </div> 
</div> 

應用程序\控制器\ password_resets_controller.rb:

class PasswordResetsController < ApplicationController 
    before_action :get_user, only: [:edit, :update] 
    before_action :valid_user, only: [:edit, :update] 

    def new 
    end 

    def create 
    @user = User.find_by(email: params[:password_reset][:email].downcase) 
    if @user 
     @user.create_reset_digest 
     @user.send_password_reset_email 
     flash[:info] = "An email is sent to you with password reset instructions" 
     redirect_to root_url 
    else 
     flash.now[:danger] = "Email address not found" 
     render 'new' 
    end 
    end 

    def edit 
    end 

    def update 
    if password_blank? 
     flash.now[:danger] = "Password can't be blank" 
     render 'edit' 
    elsif @user.update_attributes(user_params) 
     log_in @user 
     flash[:success] = "Password has been reset." 
     redirect_to @user 
    else 
     render 'edit' 
    end 
    end 

    private 

    def user_params 
     params.require(:user).permit(:password, :password_confirmation) 
    end 

    # Returns true if password is blank. 
    def password_blank? 
     params[:user][:password].blank? 
    end 

    # Before filters 

    def get_user 
     @user = User.find_by(email: params[:email]) 
    end 

    # Confirms a valid user. 
    def valid_user 
     unless (@user && @user.activated? && @user.authenticated?(:reset, params[:id])) 
     redirect_to root_url 
     end 

    # Checks expiration of reset token. 
    def check_expiration 
     if @user.password_reset_expired? 
     flash[:danger] = "Password reset has expired." 
     redirect_to new_password_reset_url 
     end 
    end 
end 

更新:在寫這個問題時,我發現當我從\ views \ shared_error_messages.html.erb中刪除<%= render 'shared/error_messages' %>行時,我不再有問題,並且新視圖顯示正確。然後它可以正常工作,我可以更改密碼。但是,錯誤消息不再顯示(例如,當密碼和密碼確認不匹配時)。該怎麼辦?

回答

0

您在呈現時必須將對象傳遞給錯誤消息模板。在調用render的錯誤信息模板現在應該包括object: f.object,就像這樣:

<%= render "shared/error_messages", object: f.object %>

0

你的局部犯規瞭解「對象」任何東西。您必須將@user對象傳遞給部分:

<%= render 'shared/error_messages', locals: {object: @user %> 
相關問題