Ruby on Rails Tuto<%if signed_in? %GT;错误

时间:2014-08-26 21:06:46

标签: ruby-on-rails session

我关注this tutorial,但我不明白为什么我在<% if signed_in? %>上遇到错误(app / views / layouts / application.html.erb)

这是我的application_controller.rb

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

我的sessions_helper.rb

module SessionsHelper

  def sign_in(user)
    remember_token = User.new_remember_token
    cookies.permanent[:remember_token] = remember_token
    user.update_attribute(:remember_token, User.digest(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
end

我错过了一些明显的东西吗?我得到了这些错误:

app/helpers/sessions_helper.rb:20:in `current_user'
app/helpers/sessions_helper.rb:11:in `signed_in?'
app/views/layouts/application.html.erb:29:in `_app_views_layouts_application_html_erb___1005071352_28922208'

编辑

SQLite3::SQLException: no such column: users.remember_token: SELECT  "users".* FROM "users"  WHERE "users"."remember_token" = 'da39a3ee5e6b4b0d3255bfef95601890afd80709' LIMIT 1

由于

1 个答案:

答案 0 :(得分:1)

事实证明我错过了本教程的一个步骤。当我执行rails generate migration add_remember_token_to_users时,我没有意识到我的迁移是空的:

class AddRememberTokenToUsers < ActiveRecord::Migration
  def change
  end
end

我只关注the advice并执行新的迁移(因为时间戳):

class AddRememberTokenToUsers < ActiveRecord::Migration
  def change
    add_column :users, :remember_token, :string
    add_index  :users, :remember_token
  end
end

感谢大家的帮助