Heroku App 崩溃错误:未初始化的常量 ApplicationController::SessionsHelper (NameError)
Heroku App Crash err: uninitialized constant ApplicationController::SessionsHelper (NameError)
我正在学习 Hartl 教程,我的 rails 应用程序在开发中运行良好,但在 heroku 中崩溃并显示以下错误代码:
application_controller.rb:5:in `<class:ApplicationController>': uninitialized constant ApplicationController::SessionsHelper (NameError)
这是在我向架构中添加 remember_digest 之后发生的。不确定是迁移还是 SessionHelper 和 ApplicationController
之间的问题
ApplicationController
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
include SessionsHelper
end
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 应用程序并启动一个新的应用程序来重置迁移(过去曾用于 "troubleshoot"),但这次没有用。当我 运行 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 () [["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 () [["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 () [["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 () [["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 () [["version", "20150204094519"]]
(4.7ms) COMMIT
我怎样才能使最后一次迁移工作?
我的问题是 git 没有跟踪我添加的任何会话文件,所以它们在我的本地计算机上但不在 github->heroku 上。我通过仅在 sessions_helper 上进行更改发现了这一点,然后提交到 git 只是为了得到一条消息,即没有对任何文件进行更改,但有几个(所有会话文件)未被跟踪。
发生是因为我使用 git -am "commit message" 而不是 git 添加 -A 首先我认为 -a 标志添加了所有内容。应该检查 git 以确保 sessions_helper.rb 在那里,所有的答案都告诉我检查这个,但我只是在我的本地机器上检查。
我正在学习 Hartl 教程,我的 rails 应用程序在开发中运行良好,但在 heroku 中崩溃并显示以下错误代码:
application_controller.rb:5:in `<class:ApplicationController>': uninitialized constant ApplicationController::SessionsHelper (NameError)
这是在我向架构中添加 remember_digest 之后发生的。不确定是迁移还是 SessionHelper 和 ApplicationController
之间的问题ApplicationController
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
include SessionsHelper
end
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 应用程序并启动一个新的应用程序来重置迁移(过去曾用于 "troubleshoot"),但这次没有用。当我 运行 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 () [["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 () [["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 () [["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 () [["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 () [["version", "20150204094519"]]
(4.7ms) COMMIT
我怎样才能使最后一次迁移工作?
我的问题是 git 没有跟踪我添加的任何会话文件,所以它们在我的本地计算机上但不在 github->heroku 上。我通过仅在 sessions_helper 上进行更改发现了这一点,然后提交到 git 只是为了得到一条消息,即没有对任何文件进行更改,但有几个(所有会话文件)未被跟踪。
发生是因为我使用 git -am "commit message" 而不是 git 添加 -A 首先我认为 -a 标志添加了所有内容。应该检查 git 以确保 sessions_helper.rb 在那里,所有的答案都告诉我检查这个,但我只是在我的本地机器上检查。