我正在开发一个简单的应用程序,它重用了Michael Hartl着名的Rail教程的Sample应用程序的一些代码。更具体地说,我正在重新使用用户模型,但已将其重命名为" Account"。我想我已经替换了所有对User模型的引用,但不知何故不能使我的代码工作。这是我的代码:
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>®</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 & 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'
换句话说,由于某种原因,会话助手无法识别Account类。当“帐户”被“用户”替换时,“教程”中的相同代码也可以使用。
有趣的是,当我决定将帐户模型包含在SessionsHelper中时(我不应该这样做,但我只是作为一个实验)我得到了
wrong argument type Class (expected Module)
您可以在此屏幕截图中找到更多详细信息:
问题是什么? 为什么SessionsHelper无法看到帐户模型?事实上,它无法看到任何模型 - 我已更换&#34;包括帐户&#34;与&#34;包括提醒&#34; (我有另一个ActiveRecord模型),我得到相同的错误消息。所有模型都应该对帮助者可见 - 为什么这不是这种情况?
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 :(得分:0)
从看起来,您要么没有更改迁移以反映帐户,要么您没有为该文件运行迁移。请分享schema.rb文件的内容。
答案 1 :(得分:0)
我最终命名为&#39; Account&#39;作为&#39;用户&#39;。现在一切都有效。还是不知道出了什么问题。似乎有某种轨道和魔法&#39;魔法&#39;与用户&#39;相关联名称。
答案 2 :(得分:0)
Account
是一个班级
SessionsHelper
是一个模块。
您无法将{/ em> Account
包含在SessionsHelper
中,因为某个类无法混入模块。反之亦然。
这就是为什么你在StaticPages#home
获得TypeError的原因。