2015-02-10 150 views
0

我做的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:再次遷移不會給出任何結果。任何關於您認爲問題可能位於何處的信息都將有所幫助,但我無法找到有關導致此問題的任何信息。

+1

確保執行'SessionsHelper'的文件名爲'sessions_helper.rb'並放置在'app/helpers'中。 – 2015-02-10 09:50:23

+0

@MarekLipka一切都還好 – 2015-02-10 09:56:48

+0

我覺得把幫手包含進控制器並不合適。這就是擔心的問題。你真的需要在你的視圖中使用'log_in'方法嗎? – BroiSatse 2015-02-10 10:39:36

回答

0

我的問題是,git沒有跟蹤我添加的任何會話文件,所以它們在我的本地計算機上,但不在github-> heroku上。我只是通過在sessions_helper上進行更改才發現了這一點,然後提交git只是爲了獲得一條消息,指出沒有任何文件發生更改,但有幾個(所有會話文件)未被跟蹤。

發生了,因爲我使用git -am "commit message"而不是git add -A第一個bc我以爲-a標誌添加了所有內容。應該檢查Git確保sessions_helper.rb在那裏,所有的答案告訴我檢查這個,但我只是檢查我的本地機器。

相關問題