Rails教程signed_in?功能不起作用

时间:2014-09-05 03:35:11

标签: ruby-on-rails railstutorial.org

我正在关注Michael Hartl的教程,在第8章中,您可以创建用户会话以进行登录。出于某种原因,signed_in?功能似乎无法正常工作 - 我总是得到#34;登录"链接,而不是"退出"用户实际登录时的链接。

知道我的代码出错了吗?

Application.html.erb(我在哪里<% if signed_in? %>):

<!DOCTYPE html>
<html>
<head>
  <title>Sample App</title>
  <%= stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" => true %>
  <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
  <%= csrf_meta_tags %>
</head>
<body>
    <div class="container">
        <% if signed_in? %>
            <%= link_to "Sign out", signout_path, method: "delete" %>
        <% else %>
            <%= link_to "Sign in", signin_path %>
        <% end %>
      <% flash.each do |key, value| %>
        <div class="alert alert-<%= key %>"><%= value %></div>
      <% end %>
      <%= yield %>
      <%= debug(params) if Rails.env.development? %>
    </div>
</body>
</html>

会话助手:

module SessionsHelper

  def sign_in(user)
    remember_token = User.new_remember_token
    cookies.permanent[:remember_token] = remember_token
    user.update_attributes(remember_token: remember_token)
    self.current_user = user
  end

  def signed_in?
    !current_user.nil?
  end

  def current_user=(user)
    @current_user = user
  end

  def current_user
    remember_token = User.digest(cookies[:remember_token])
    @current_user ||= User.find_by(remember_token: remember_token)
  end

  def current_user?(user)
    user == current_user
  end

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

Application Controller(其中添加了会话助手):

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  include SessionsHelper
end

会话控制器:

class SessionsController < ApplicationController

  def new
  end

  def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
      sign_in user
      redirect_to user
    else
      flash.now[:error] = 'Invalid email/password combination' 
      render 'new'
    end
  end

  def destroy
    sign_out
    redirect_to root_url
  end
end

用户控制器:

class UsersController < ApplicationController
  def new
    @user = User.new
  end

  def show
    @user = User.find(params[:id])
  end

  def create
    @user = User.new(user_params)
    if @user.save
      sign_in @user
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      render 'new'
    end
  end

  private

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

我认为我最大的绊脚石一直在试图弄清楚如何实际调试和测试1.用户实际上是在登录,2。是signed_in?功能正在解雇,但我似乎无法弄清楚如何调试这个。关于如何做到这一点的任何提示/指示也将非常感谢!我试图将内容记录到Chrome控制台,但它似乎只是javascript而且rails无法登录。

编辑:添加模型

class User < ActiveRecord::Base
  before_save { self.email = email.downcase }
  before_create :create_remember_token

  validates :username, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(?:\.[a-z\d\-]+)*\.[a-z]+\z/i
  validates :email, presence:   true,
                    format:     { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  has_secure_password
  validates :password, length: { minimum: 6 }

  def User.new_remember_token
    SecureRandom.urlsafe_base64
  end

  def User.digest(token)
    Digest::SHA1.hexdigest(token.to_s)
  end

  private

    def create_remember_token
        self.remember_token = User.digest(User.new_remember_token)
    end
end

1 个答案:

答案 0 :(得分:1)

我不熟悉该教程,但问题似乎在这里:

def sign_in(user)
  remember_token = User.new_remember_token
  cookies.permanent[:remember_token] = remember_token
  user.update_attributes(remember_token: remember_token) # 1
  self.current_user = user
end

def current_user
  remember_token = User.digest(cookies[:remember_token]) # 2
  @current_user ||= User.find_by(remember_token: remember_token)
end
  1. 您在用户中保存了一个值(remember_token
  2. 但您使用其他值来查看用户(digest(remember_token))。
  3. 这就是为什么你找不到当前用户的原因。我想。

    调试提示

    1. 使用pry
    2. 使用pry-debugger
    3. 在代码中放入大量的调试打印语句并查看日志(以确保它们输出您期望的内容)。我的意思是服务器日志,当然(log/development.log
    4. 如果他们没有输出您期望的内容,请将binding.pry放入其中并放入交互式调试器中。它基本上是代码执行过程中的控制台(您可以评估和更改内容,逐行逐步执行代码)