yaboiduke
yaboiduke

Reputation: 670

user.name returns :name key - Error in Rails App

I'm following Michael Hartl's Ruby on Rails Tutorial Book.Running user.name returns the :name key in my rails console instead of the "MyName" value.The console commands are;

require user.rb 
 => true 
user = User.new(:name =>"Myname", :email=> "[email protected]")
user.name   
=>[:name]
user.email
=>[:email]

As mentioned, running user.name or user.email returns [:name] and [:email] respectively.What could be the problem?

class User    
  attr_accessor :name, :email

  def initialize (attributes = {})
    @name = [:name]
    @email = [:email]
  end

  def formatted_email
    "#{@name} <#{@email}>"
  end 
end

Upvotes: 0

Views: 50

Answers (1)

Marek Lipka
Marek Lipka

Reputation: 51191

You should have:

@name = attributes[:name]
@email = attributes[:email]

in your User#initialize method.

Upvotes: 4

Related Questions