0

我正在使用refinerycms 并覆盖重置密码的功能。这是我的密码查看页面:

密码/new.html.erb

<h2>Forgot your password?</h2>
<%= form_for(resource, :as => resource_name, :url => refinery.user_password_path, :html => { :method => :post }) do |f| %>

<div><%= f.label :email %><br />
<%= f.email_field :email %></div>

<div><%= f.submit "Send me reset password instructions" %></div>
<% end %>

密码/edit.html.erb

<h2>Change your password</h2>   

<%= form_for(resource, :as => resource_name, :url => refinery.user_password_path, :html => { :method => :put }) do |f| %>
<%= f.hidden_field :reset_password_token %>

<div><%= f.label :password, "New password" %><br />
<%= f.password_field :password %></div>

<div><%= f.label :password_confirmation, "Confirm new password" %><br />
<%= f.password_field :password_confirmation %></div>

<div><%= f.submit "Change my password" %></div>
<% end %>

密码控制器是:

 class PasswordsController < Devise::PasswordsController
   helper Refinery::Core::Engine.helpers
   before_filter :store_password_reset_return_to, :only => [:update]

   def store_password_reset_return_to
     session[:'refinery_user_return_to'] = refinery.admin_root_path
   end
   protected :store_password_reset_return_to

   # Rather than overriding devise, it seems better to just apply the notice here.
   after_filter :give_notice, :only => [:update]
   def give_notice
     Rails.logger.debug @refinery_user
     if %w(notice error alert).exclude?(flash.keys.map(&:to_s)) or             @refinery_user.errors.any?
    flash[:notice] = t('successful', :scope => 'users.reset', :email => @refinery_user.email)
     end
  end
   protected :give_notice

   # GET /registrations/password/edit?reset_password_token=abcdef
   def edit
    if params[:reset_password_token] and (  @refinery_user = Refinery::User.where(:reset_password_token => params[:reset_password_token]).first).present?
      #Rails.logger.debug @refinery_user
      #logger.debug"******************************************************"
      respond_with(@refinery_user)
    else
      redirect_to refinery.new_user_password_path,
                :flash => ({ :error => t('code_invalid', :scope => 'refinery.users.reset') })
    end
  end

    # POST /registrations/password
    def create
    if params[:user].present? and (email = params[:user][:email]).present? and (user = Refinery::User.where(:email => email).first).present?

      # Call devise reset function.
      user.send(:generate_reset_password_token!)
      UserMailer.reset_notification(user, request).deliver
      redirect_to refinery.new_user_session_path,:notice => t('email_reset_sent', :scope => 'users.forgot')
    else
        flash.now[:error] = if (email = params[:user][:email]).blank?
        t('blank_email', :scope => 'users.forgot')
      else
        t('email_not_associated_with_account_html', :email => ERB::Util.html_escape(email), :scope => 'users.forgot').html_safe
      end
      render :new
    end
  end
end

邮件已成功通过重置通知和编辑密码链接发送电子邮件,但是当我输入新密码并按 Enter 时,它会给出错误
“未定义的方法email' for nil:NilClass",app/controllers/passwords_controller.rb:15:ingive_notice”。我怎样才能删除该错误??请帮助!!

4

1 回答 1

0

The result is to simply remove give_notice method and use error message field in password/edit.html.erb as:

<% if resource.try(:errors) %>
  <%= devise_error_messages! %>
<% end %>
于 2012-09-06T08:24:04.443 回答