0

我正在完成一个基于 Hartl 教程的项目。我已经设置了一个有效的用户身份验证模型......主要是。用户可以注册,但我在登录过程中遇到问题。如果用户退出,他们就不可能登录。我已经验证数据库正在保存用户的注册信息,所以问题在于确认用户已经注册。我查看了我的日志,但它们没有帮助。

这就是我得到的:

  Started POST "/sessions" for 127.0.0.1 at 2012-04-15 13:56:46 -0500
    Processing by SessionsController#create as HTML
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"itOIPKPrXlymcBujKMu4Xjwvs6GlD3jteBQJf+/mYEY=", "session"=>{"email"=>"tester3@tester3.com", "password"=>"[FILTERED]"}, "commit"=>"Sign in"}
    User Load (0.3ms)  SELECT `users`.* FROM `users` WHERE `users`.`email` = 'tester3@tester3.com' LIMIT 1
  Rendered sessions/new.html.erb within layouts/application (1.6ms)
  Rendered layouts/_stylesheets.html.erb (2.0ms)
    User Load (0.3ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` IS NULL LIMIT 1
    CACHE (0.0ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` IS NULL LIMIT 1
  Rendered layouts/_header.html.erb (3.2ms)
  Rendered layouts/_footer.html.erb (0.3ms)
  Completed 200 OK in 30ms (Views: 16.1ms | ActiveRecord: 2.6ms)

但我应该看到的是这个。

  Started POST "/sessions" for 127.0.0.1 at 2012-04-15 12:50:24 -0500

    Processing by SessionsController#create as HTML

    Parameters: {"utf8"=>"✓", "authenticity_token"=>"h/M5VYBaG16sGiGHTWo26GJSU1/TlMNFjQd5TN1VZ3Y=", "session"=>{"email"=>"tester3@tester3.com", "password"=>"[FILTERED]"}, "commit"=>"Sign in"}

    User Load (0.3ms)  SELECT `users`.* FROM `users` WHERE `users`.`email` = 'tester3@tester3.com' LIMIT 1

    User Load (0.2ms)  SELECT `users`.* FROM `users` WHERE `users`.`email` = 'tester3@tester3.com' LIMIT 1[0m

  Redirected to http://localhost:3000/users/103

  Completed 302 Found in 19ms

我的问题是:解决这个问题的最佳方法是什么?
我应该开始寻找哪些“明显”的地方。我正在使用此处找到的用户身份验证代码,并且我已经在其他项目中成功实现了它。就是这一个给我带来了麻烦。这是我的验证码。

module SessionsHelper

  def sign_in(user)
    cookies.permanent.signed[:remember_token] = [user.id, user.salt]
    self.current_user = user
  end

  def current_user=(user)
    @current_user = user
  end

  def current_user
    @current_user ||= user_from_remember_token
  end

  def signed_in?
    current_user.present?
    p "user signed_in? method called" 
    p current_user
  end

  def sign_out
    cookies.delete(:remember_token)
    self.current_user = nil
    p "user has signed out" #Method does NOT get called
  end



  def current_user?(user)
    user == current_user
  end

  def authenticate
    deny_access unless signed_in?
  end

  def deny_access
    store_location
    redirect_to signin_path, :notice => "Please sign in to access this page."
  end

  def redirect_back_or(default)
    redirect_to(session[:return_to] || default)
    clear_return_to
  end

  private

    def user_from_remember_token
       p "Looking user up from the cookie"
      User.authenticate_with_salt(*remember_token)
    end

    def remember_token
      cookies.signed[:remember_token] || [nil, nil]
    end

    def store_location
      session[:return_to] = request.fullpath
    end

    def clear_return_to
      session[:return_to] = nil
    end
end
4

1 回答 1

2

你要求很明显,所以这里很明显......

您可以在操作开始时打印出 cookie 值。

您可以在操作的各个点引发异常。我喜欢这样做。它比“p”更可靠,因为打印可以通过缓冲输出。我将要打印的信息放入异常的字符串中。开发模式还吐出有用的信息,如参数。

于 2012-04-15T19:34:18.953 回答