1

こんにちは、パスワードをリセットしようとすると、次のエラーが表示されます。

PasswordResetsController#create の ArgumentError

引数の数が間違っています (0 に対して 1)

app/models/user.rb:48: create_remember_token app/models/user.rb:40:insend_password_reset 内 app/controllers/password_resets_controller.rb:7:`create' 内

class PasswordResetsController < ApplicationController
  def new
  end

  def create
    user = User.find_by_email(params[:email])
    user.send_password_reset if user
    redirect_to root_url
  end

  def edit
    @user = User.find_by_password_reset_token!(params[:id])
  end

  def update
    @user = User.find_by_password_reset_token!(params[:id])
    if @user.password_reset_sent_at < 2.hours.ago
      redirect_to new_password_reset_path, :alert => "Password &crarr; 
        reset has expired."
    elsif @user.update_attributes(params[:user])
      redirect_to root_url, :notice => "Password has been reset."
    else
      render :edit
    end
end
end



class User < ActiveRecord::Base
  attr_accessible :email, :lname, :name, :password, :password_confirmation
  has_secure_password
  before_save { |user| user.email = email.downcase }
  before_save :create_remember_token

  validates :name, presence: true, length: { maximum: 50 }
  validates :lname, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  validates :password, presence: true, :on => :create
  validates :password, length: { minimum: 6 }, :allow_blank => true
  validates :password_confirmation, presence: true, :on => :create



  def send_password_reset
    create_remember_token(:password_reset_token)
    self.password_reset_sent_at = Time.zone.now
    save!
    UserMailer.password_reset(self).deliver
  end

  private

    def create_remember_token
      self.remember_token = SecureRandom.urlsafe_base64
    end
end

理由は誰にもありますか?

4

2 に答える 2

3

create_remember_token のメソッド定義がパラメーターを受け入れない場合、create_remember_token(:password_reset_token) を呼び出しています。

つまり変わる

create_remember_token(:password_reset_token)

create_remember_token

send_password_reset メソッドで

于 2012-11-08T14:33:43.673 に答える
1

Railscastsで解決策を見つけました

before_save remember_token に追加、create_remember_token を変更

わかんないよね?しかし、それは動作します

    class User < ActiveRecord::Base
  attr_accessible :email, :lname, :name, :password, :password_confirmation
  has_secure_password

  before_save { |user| user.email = email.downcase }
  before_save { create_remember_token(:remember_token) }

  validates :name, presence: true, length: { maximum: 50 }
  validates :lname, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  validates :password, presence: true, :on => :create
  validates :password, length: { minimum: 6 }, :allow_blank => true
  validates :password_confirmation, presence: true, :on => :create

  def send_password_reset
    create_remember_token(:password_reset_token)
    self.password_reset_sent_at = Time.zone.now
    save!
    UserMailer.password_reset(self).deliver
  end



    def create_remember_token(column)
     begin
      self[column] = SecureRandom.urlsafe_base64
     end while User.exists?(column => self[column])
    end
end
于 2012-11-09T09:40:42.380 に答える