0

I'm following the tutorial in Railscast to create a "forgot my password" link. I'm running into problems when creating a cookie for the password reset. Here is the error:

ArgumentError in PasswordResetsController#create 
wrong number of arguments (1 for 0)
Rails.root: C:/Sites/application

Application Trace | Framework Trace | Full Trace
app/models/user.rb:37:in `create_remember_token'
app/models/user.rb:29:in `send_password_reset'
app/controllers/password_resets_controller.rb:7:in `create'

../app/models/user.rb - line 28 is remember_token(:password_reset_token)

before_save :create_remember_token
before_create { create_remember_token(:remember_token) }

def send_password_reset
  create_remember_token(:password_reset_token)
  save!
  UserMailer.password_reset(self).deliver
end

private
def create_remember_token
  self.remember_token = SecureRandom.urlsafe_base64
end

../app/controllers/password_resets_controller.rb

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, :notice => "Email sent with password reset instructions."
  end

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

../app/helpers/sessions_helper.rb

def sign_in(user)
  cookies.permanent[:remember_token] = user.remember_token
  current_user = user
end

def current_user
  @current_user ||= user_from_remember_token
end

def sign_out
  current_user = nil
  cookies.delete(:remember_token)
end

private

  def user_from_remember_token
    remember_token = cookies[:remember_token]
    User.find_by_remember_token(remember_token) unless remember_token.nil?
  end
end

If I change the way I find the user in my controller to: user = User.find_by_email(params[:session][:email]) the first error dissapears. I can only think that this is telling me that I'm calling the user incorrectly, and not assigning the new token to it on reset.

4

1 回答 1

1

看看 Railscast,你有“remember_token”方法,他们使用添加到 User 类的“generate_token”方法。当然,这种错字可能不是问题,但如果是错误消息,那将是有意义的(以前不存在的方法不会期望任何参数)。

于 2012-04-30T11:48:11.810 回答