0

I'm trying to display a user's full name through the view template.

 <%= @status.user.full_name %>

Above code will give the undefined local variable or method `first_name'. I have set up the "full_name" method like this through devise gem generated user.rb model.

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name,
                        :last_name, :profile_name

  has_many :statuses

  def full_name
    first_name + " " + last_name
  end
end

Lastly, first_name and last_name is defined through a devise migration file.

class DeviseCreateUsers < ActiveRecord::Migration
  def change
    create_table(:users) do |t|
      t.string :first_name
      t.string :last_name
      t.string :profile_name

My guess is that attr_accessible causes an error with devise. Need your help! Thank you!

1 Answers1

0

Use self to reference first_name and last_name of instance accessing them:

def full_name
  self.first_name + " " + self.last_name
end

Update:

Thanks to OP for accepting the answer and also thanks to @JörgWMittag for pointing out how this answer was misleading in that the question was about Rails 4 and the originally suggested answer was assuming Rails 3.

The solution for this specific problem would be to remove the attr_accessible declaration from model altogether and use Strong Parameters. The suggested link which turned this answer to "Accepted Answer", I suppose, is Adding custom fields to your Devise User model in Rails.

Thanks to @JörgWMittag again for pointing out the misuse of self in the original answer. self is not required on first_name and last_name in the full_name method above. Had this method been a getter or setter then the use of self keyword would have been necessary to imply that we want to operate on an instance's attribute and not a local variable in the function.

There seem to be plenty of resources here in StackOverflow regarding this topic but here is a well accepted answer with clear distinction: When to use 'self' in Ruby

Community
  • 1
  • 1
vee
  • 38,255
  • 7
  • 74
  • 78
  • Thanks. But the error is still there :( I can't figure it out. – Hyunsoo Choi Feb 03 '14 at 23:14
  • @HyunsooChoi, any useful information in the log? Just tested this in `Rails 3.2` + `Devise 3.0` and it is working as expected. – vee Feb 03 '14 at 23:19
  • Yeah I honestly think it has to do with the Rails 4.2 + devise 3.0. I don't think they are fully compatible. Let me check the log. Thanks – Hyunsoo Choi Feb 03 '14 at 23:21
  • If you're on Rails4 shouldn't you be using strong parameters and not `attr_accessible`? – vee Feb 03 '14 at 23:23
  • You are right. I'm not so familiar with strong parameters. Could you kindly provide me with an example? – Hyunsoo Choi Feb 03 '14 at 23:28
  • Google yields [Adding custom fields to your Devise User model in Rails](http://blog.12spokes.com/web-design-development/adding-custom-fields-to-your-devise-user-model-in-rails-4/) around the top which looks good and solves your requirement. – vee Feb 03 '14 at 23:30
  • Ah! Thank you so much @vee. This explains it very well. Thanks. – Hyunsoo Choi Feb 03 '14 at 23:40
  • 1
    `self` is always the implied receiver in Ruby, there is no need to explicitly specify it. If the code in the question doesn't work, then the code in this answer cannot possibly work either. And if the code in this answer *does* work then the code in the question should too. Either way, this answer is at best misleading. – Jörg W Mittag Feb 04 '14 at 02:16
  • @JörgWMittag, agree with you on "misleading", comment turned answer. I wasn't aware that one can't delete an accepted answer :) – vee Feb 04 '14 at 02:39
  • @JörgWMittag, thank you for clarification on use of `self`. I would certainly appreciate any feedback on my update on use of `self` in a class. – vee Feb 04 '14 at 03:06