我做的Hartl的教程和我的Rails應用程序工作在發展很好,但在崩潰的Heroku此錯誤代碼:未初始化的常量SessionsHelper(NameError)
application_controller.rb:5:in `<class:ApplicationController>': uninitialized constant ApplicationController::SessionsHelper (NameError)
這件事發生後,我加入了remember_digest到架構。不知道是否是遷移或
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和SessionsHelper之間ApplicationController的問題
的ApplicationController並開始一個新的重置遷移(過去一直努力「排除故障」),但這次並沒有奏效。當我運行heroku rake db:migrate
時,除了記住的摘要之外,所有的遷移都顯示出來。我跑db:再次遷移但不能遷移。下面是遷移:
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
遷移日誌
[email protected]:~/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:再次遷移不會給出任何結果。任何關於您認爲問題可能位於何處的信息都將有所幫助,但我無法找到有關導致此問題的任何信息。
確保執行'SessionsHelper'的文件名爲'sessions_helper.rb'並放置在'app/helpers'中。 – 2015-02-10 09:50:23
@MarekLipka一切都還好 – 2015-02-10 09:56:48
我覺得把幫手包含進控制器並不合適。這就是擔心的問題。你真的需要在你的視圖中使用'log_in'方法嗎? – BroiSatse 2015-02-10 10:39:36