2014-11-21 53 views
0

我正在开发一个简单的应用程序,它重用了Michael Hartl着名的Rail Tutorial的示例应用程序的一些代码。更具体地说,我重新使用用户模型,但将其重新命名为“帐户”。我想我已经取代了用户模型的所有引用,但不知何故无法让我的代码工作。这里是我的代码:Rails教程:会话帮助中的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离子助手无法识别Account类。当帐户被用户取代时,教程中的相同代码将起作用。

有趣的是,当我决定包括在SessionsHelper帐户模型(我不应该需要做的,但我这样做只是作为一个实验)我得到

wrong argument type Class (expected Module) 

你可以找到更多细节截图:

enter image description here

什么问题? 为什么SessionsHelper无法查看帐户模型?事实上,它看不到任何模型 - 我用“include Reminder”(另一个ActiveRecord模型)替换了“include Account”,并且我得到了相同的错误消息。所有的模型对助手都是可见的 - 为什么这里不是这种情况?

P.S.我没有跑迁移,但我不认为这个问题是存在的,但这里是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

我最终将'Account'命名为'User'。现在一切正常。仍然不知道什么是错的。似乎有某种与'用户'名字相关的'魔术'。

0

从它似乎什么,要么你还没有改变的迁移,以反映帐号或者你还没有运行该文件的迁移。请分享您的schema.rb文件的内容。

+0

感谢您的回复 - 只是做了它 – Nick 2014-11-21 19:13:13

0

Account是一类。
SessionsHelper是一个模块。

不能包括AccountSessionsHelper因为一类不能混合在的模块。这是相反的。

这就是为什么你得到TypeError StaticPages#home