未初始化的常量SessionsHelper(NameError)

时间:2015-02-10 09:48:11

标签: ruby-on-rails ruby ruby-on-rails-4 railstutorial.org

我正在进行Hartl教程,我的rails应用程序在开发中运行良好,但在heroku中崩溃时出现此错误代码:

application_controller.rb:5:in `<class:ApplicationController>': uninitialized constant ApplicationController::SessionsHelper (NameError)

这是在我将一个remember_digest添加到架构后发生的。不确定它是迁移还是SessionsHelper和ApplicationController之间的问题

的ApplicationController

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  include SessionsHelper
end
位于app / helpers中的

sessions_helper.rb

module SessionsHelper

  # Logs in the given user.
  def log_in(user)
    session[:user_id] = user.id
  end

  # Remembers a user in a persistent session.
  def remember(user)
    user.remember
    cookies.permanent.signed[:user_id] = user.id
    cookies.permanent[:remember_token] = user.remember_token
  end

  # Returns the user corresponding to the remember token cookie.
  def current_user
    if (user_id = session[:user_id])
      @current_user ||= User.find_by(id: user_id)
    elsif (user_id = cookies.signed[:user_id])
      user = User.find_by(id: user_id)
      if user && user.authenticated?(cookies[:remember_token])
        log_in user
        @current_user = user
      end
    end
  end

  # Returns true if the user is logged in, false otherwise.
  def logged_in?
    !current_user.nil?
  end

  def forget(user)
    user.forget
    cookies.delete(:user_id)
    cookies.delete(:remember_token)
  end

  # Logs out the current user.
  def log_out
    forget(current_user)
    session.delete(:user_id)
    @current_user = nil
  end
end

我尝试删除旧的heroku应用程序并启动一个新应用程序以重置迁移(过去曾经“排除故障”),但这次没有用。当我运行heroku rake db:migrate时,除了记忆摘要之外,所有迁移都会出现。我再次运行db:migrate但是无法迁移它。以下是迁移:

Migrate.db

20150204074511_create_users.rb                  20150204093042_add_phone_number_to_users.rb
20150204081616_add_index_to_users_email.rb      20150204094519_add_index_to_users_phone_number.rb
20150204081750_add_password_digest_to_users.rb  20150207093225_add_remember_digest_to_users.rb

迁移日志

ajhausdorf@rails-tutorial:~/workspace/AccessOBD (master) $ heroku run rake db:migrate
Running `rake db:migrate` attached to terminal... up, run.4474
   (18.0ms)  CREATE TABLE "schema_migrations" ("version" character varying NOT NULL) 
   (8.6ms)  CREATE UNIQUE INDEX  "unique_schema_migrations" ON "schema_migrations"  ("version")
  ActiveRecord::SchemaMigration Load (1.5ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Migrating to CreateUsers (20150204074511)
   (0.9ms)  BEGIN
== 20150204074511 CreateUsers: migrating ======================================
-- create_table(:users)
   (15.1ms)  CREATE TABLE "users" ("id" serial primary key, "name" character varying, "email" character varying, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL) 
   -> 0.0162s
== 20150204074511 CreateUsers: migrated (0.0164s) =============================

  SQL (1.2ms)  INSERT INTO "schema_migrations" ("version") VALUES ($1)  [["version", "20150204074511"]]
   (4.0ms)  COMMIT
Migrating to AddIndexToUsersEmail (20150204081616)
   (0.8ms)  BEGIN
== 20150204081616 AddIndexToUsersEmail: migrating =============================
-- add_index(:users, :email, {:unique=>true})
   (4.6ms)  CREATE UNIQUE INDEX  "index_users_on_email" ON "users"  ("email")
   -> 0.0081s
== 20150204081616 AddIndexToUsersEmail: migrated (0.0082s) ====================

  SQL (0.9ms)  INSERT INTO "schema_migrations" ("version") VALUES ($1)  [["version", "20150204081616"]]
   (5.5ms)  COMMIT
Migrating to AddPasswordDigestToUsers (20150204081750)
   (0.7ms)  BEGIN
== 20150204081750 AddPasswordDigestToUsers: migrating =========================
-- add_column(:users, :password_digest, :string)
   (1.3ms)  ALTER TABLE "users" ADD "password_digest" character varying
   -> 0.0022s
== 20150204081750 AddPasswordDigestToUsers: migrated (0.0023s) ================

  SQL (0.8ms)  INSERT INTO "schema_migrations" ("version") VALUES ($1)  [["version", "20150204081750"]]
   (1.7ms)  COMMIT
Migrating to AddPhoneNumberToUsers (20150204093042)
   (0.7ms)  BEGIN
== 20150204093042 AddPhoneNumberToUsers: migrating ============================
-- add_column(:users, :phone, :string)
   (1.4ms)  ALTER TABLE "users" ADD "phone" character varying
   -> 0.0023s
== 20150204093042 AddPhoneNumberToUsers: migrated (0.0024s) ===================

  SQL (0.9ms)  INSERT INTO "schema_migrations" ("version") VALUES ($1)  [["version", "20150204093042"]]
   (2.8ms)  COMMIT
Migrating to AddIndexToUsersPhoneNumber (20150204094519)
   (4.4ms)  BEGIN
== 20150204094519 AddIndexToUsersPhoneNumber: migrating =======================
-- add_index(:users, :phone, {:unique=>true})
   (7.5ms)  CREATE UNIQUE INDEX  "index_users_on_phone" ON "users"  ("phone")
   -> 0.0110s
== 20150204094519 AddIndexToUsersPhoneNumber: migrated (0.0111s) ==============

  SQL (0.8ms)  INSERT INTO "schema_migrations" ("version") VALUES ($1)  [["version", "20150204094519"]]
   (4.7ms)  COMMIT

这种迁移是否有可能无法通过?再次运行db:migrate不会产生任何结果。任何关于您认为问题可能位于何处的信息都会有所帮助,我找不到任何导致此问题的信息。

1 个答案:

答案 0 :(得分:0)

我的问题是git没有跟踪我添加的任何会话文件,因此它们在我的本地计算机上,但不在github-&gt; heroku上。我通过仅在sessions_helper上进行更改找到了这一点,然后提交git只是为了得到一条消息,即没有对任何文件进行任何更改,但有几个(所有会话文件)都未被跟踪。

发生了,因为我先使用git -am "commit message"而不是git add -A bc我认为-a标志添加了所有内容。应该检查git以确保sessions_helper.rb在那里,所有的答案都告诉我检查这个,但我只是检查我的本地机器。