2014-11-21 3 views
0

나는 Michael Hartl의 유명한 레일 튜토리얼의 샘플 애플리케이션 코드를 재사용 한 간단한 애플리케이션을 개발 중이다. 더 구체적으로, 나는 User 모델을 재사용하고 있지만 "Account"라는 이름을 다시 붙였다. User 모델에 대한 모든 참조를 대체했지만 어떻게 든 내 코드를 작동시킬 수는 없다고 생각합니다. 여기레일 튜토리얼 : sessions_helper의 NameError

class Account < ActiveRecord::Base 
include Person 
include Contact 
has_many :coworkers, :class_name => 'Coworker' 
has_many :customers, :class_name => 'Customer' 
has_many :locations, :class_name => 'Location' 
has_many :appointment_types, :class_name => 'AppointmentType' 
before_save { self.email = email.downcase } 
has_secure_password 
attr_accessor :remember_token 
validates :password, length: { minimum: 6 } 
# rem_notice_hrs 
validates :rem_notice_hrs, presence: true 
validates :rem_notice_hrs, numericality: true 
# rem_text 
validates :rem_text, presence: true 
# mandatory email: 
validates :email, presence: true, length: { maximum: 255 }, 
     format: { with: VALID_EMAIL_REGEX } 

after_initialize :init 

private 

def init 
    if self.new_record? 
    if self.rem_notice_hrs.nil? 
     self.rem_notice_hrs = 24 
    end 
    if self.rem_text.nil? 
     if self.company.nil? 
     self.rem_text = "Dear [customer title: automatic] [customer family name: automatic], this is a reminder of your appointment with %{title} %{family_name} on [date/time]." 
     else 
     self.rem_text = "Dear [title] [customer family name], this is a reminder of your appointment with %{company} on [date/time]." 
     end 
    end 
    if self.start_day.nil? 
     self.start_day = Time.now 
    end 
    end 
end 
end 

세션 도우미입니다 : 여기

module SessionsHelper 

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

# Returns the current logged-in user (if any). 
def current_account 
@current_account ||= Аccount.find_by(id: session[:account_id]) 
end 

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

부분 헤더입니다 : 여기 내 코드는

<header class="navbar navbar-default navbar-fixed-top"> 
<div class="container"> 
<a class="navbar-brand" href=<%= root_path %>><font color="red">Sample Application<sup>&reg;</sup></font></a> 
<nav> 
    <ul class="nav navbar-nav"> 
    <% if logged_in? %> 
    <% else %> 
     <li class="active"><a href=<%= root_path %>>Home</a></li> 
     <li><a href=<%= demo_path %>>Try Demo</a></li> 
     <li><a href=<%= pricing_path %>>Pricing &amp; Sign up</a></li> 
     <li><a href="#login">Login</a></li> 
    <% end %> 
    </ul> 
</nav> 
</div> 
</header> 

나는

NameError in StaticPages#home 
Showing /Users/nnikolo/Documents/private/rails_projects/appmate/app/views/layouts/_header.html.erb where line #6 raised: 

undefined local variable or method `Аccount' for #<#<Class:0x007fbc1ede96f8>:0x007fbc1ede8b18> 
app/helpers/sessions_helper.rb:10:in `current_account' 
app/helpers/sessions_helper.rb:15:in `logged_in?' 
app/views/layouts/_header.html.erb:6:in `_app_views_layouts__header_html_erb___3728230762564015047_70222988074560' 
app/views/layouts/application.html.erb:20:in `_app_views_layouts_application_html_erb___3720454973504965845_70222923917160' 
을 얻고 코드를 실행하면

즉, 웬일인지 Sess ion 도우미가 Account 클래스를 인식하지 못합니다. 계정이 사용자로 대체되면 자습서의 동일한 코드가 작동합니다. 이에서 자세한 내용을 찾을 수 있습니다 나는 SessionsHelper의 계정 모델을 포함하기로 결정했다 (내가 할 필요가 없습니다하지만 난 그냥 실험로했다) 때

흥미롭게도, 내가

wrong argument type Class (expected Module) 

무엇입니까 스크린 샷 :

enter image description here

문제는 무엇입니까? SessionsHelper가 계정 모델을 볼 수없는 이유는 무엇입니까? 사실, 모델 중 하나를 볼 수 없습니다. "include Account"를 "Include Reminder"(내가 가진 다른 ActiveRecord 모델)로 바꿨고 동일한 오류 메시지가 나타납니다. 모든 모델이 도우미에게 보여야합니다 - 왜 여기에 해당하지 않는가?

P. 나는 마이그레이션을 실행 않았고 나는 문제가 있지만 여기 schema.rb의 관련 섹션 생각하지 않습니다 : 그것은 보이는에서

create_table "accounts", force: true do |t| 
    t.string "password_digest",        null: false 
    t.string "remember_digest" 
    t.string "activation_digest" 
    t.boolean "activated",      default: false 
    t.datetime "activated_at" 
    t.string "title" 
    t.string "first_name" 
    t.string "last_name" 
    t.string "company" 
    t.string "email",    limit: 100,     null: false 
    t.integer "phone",    limit: 8,     null: false 
    t.integer "rem_notice_hrs",        null: false 
    t.string "rem_text",   limit: 140,     null: false 
    t.datetime "start_day",          null: false 
    t.datetime "end_day" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

답변

0

'계정'을 '사용자'로 지정했습니다. 모든 것이 지금 작동합니다. 아직도 무엇이 잘못되었는지 모릅니다. '사용자'이름과 관련된 일종의 레일 '마술'이있는 것처럼 보입니다.

0

, 중 당신은 계정을 반영하기 위해 마이그레이션을 변경 havent 한 또는 당신이를 havent 해당 파일에 대한 이주를 실행하십시오. schema.rb 파일의 내용을 공유하십시오.

+0

답변 해 주셔서 감사합니다. 그냥 해 주셨습니다. – Nick

0

Account은 클래스입니다.
SessionsHelper은 모듈입니다. 클래스가 혼합에 모듈이 될 수 없기 때문에

당신은 SessionsHelperAccount을 포함 할 수 없습니다. 그것은 다른 방향입니다.

그런 이유로 TypeError가 StaticPages#home에 발생했습니다.