Rails Tutorial: NameError in sessions_helper

156 Views Asked by At

I am developing a simple application which re-used some of the code of the Sample application of the famous Rail Tutorial of Michael Hartl. More specifically, I am re-using the User model but have re-named it as "Account". I think I have replaced all the references to the User model but somehow can't make my code work. Here is my code:

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

Here is the Session helper:

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

Here is the header partial:

<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>

When I run the code I am getting

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'

In other words, for some reason the Session helper cannot recognise the Account class. The same code in the Tutorial works when Account is replaced by User.

Interestingly, when I decided to include the Account model in the SessionsHelper (which I should not need do but I did it just as an experiment) I am getting

wrong argument type Class (expected Module)

You can find more details in this screenshot:

enter image description here

What's the problem? Why can't SessionsHelper see the Account model? In fact, it cannot see any of the models - I replaced "include Account" with "include Reminder" (another ActiveRecord model I have) and I get the same error message. All the models shall be visible to the helper - why is this not the case here?

P.S. I did run migration and I don't think the problem is there but here is the relevant section of the 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
3

There are 3 best solutions below

0
Nick On BEST ANSWER

I ended up naming 'Account' as 'User'. Everything works now. Still don't know what was wrong. Seems like there is some sort of rails 'magic' associated with the 'User' name.

1
Ruby_Pry On

From what it seems, either you havent changed the migration to reflect Account or you havent run the migration for that file. Please share the contents of your schema.rb file.

0
adeluccar On

Account is a class.
SessionsHelper is a Module.

You can't include Account into SessionsHelper because a Class can't be mixed-in a Module. It's the other way round.

That's why you're getting that TypeError at StaticPages#home.