Kishore Eechambadi
Kishore Eechambadi

Reputation: 117

undefined local variable or method `user

Fairly new to RoR, and working my way through a tutorial. Not sure why I'm getting this error, since I have a user model that defines first_name as an attr accessible (it says first_name isn't a defined method if I capitalize User).

The error: undefined local variable or method `user' for #<#:0x007fdb75963478> Extracted source (around line #9):

6: 
7: <% @statuses.each do |status| %>
8:   <div class ="status">
9:     <strong><%= user.first_name %></strong>
10:     <p><%= status.content %></p>
11:   <div class ="meta">
12:     <%= link_to time_ago_in_words(status.created_at) + " ago", status %>

My Code: User Model:

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

# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me,
:first_name, :last_name, :profile_name
# attr_accessible :title, :body

validates :first_name, presence: true 
validates :last_name, presence: true 
validates :profile_name, presence: true, uniqueness: true, 
format: {
with: /a-zA-Z0-9_-/, 
message: 'Must be formatted correctly.'
} 

def full_name
return first_name + " " + last_name
end

has_many :statuses

end

Index.html.erb:

<div class="page-header">
<h1>All of the statuses</h1>
</div>

<%= link_to "Post a new Status", new_status_path, class: "btn btn-success" %>

<% @statuses.each do |status| %>
<div class ="status">
<strong><%= user.first_name %></strong>
<p><%= status.content %></p>
<div class ="meta">
<%= link_to time_ago_in_words(status.created_at) + " ago", status %>
<span class="admin">
  | <%= link_to "Edit", edit_status_path(status) %> |
  <%= link_to "Delete", status, method: :delete, data: {confirm: "Are you sure you wanna  delete this status"} %>
</span>
</div>
</div>  
<% end %>  

Upvotes: 0

Views: 493

Answers (1)

Rahul Tapali
Rahul Tapali

Reputation: 10137

On line #9 it should be:

 <%= status.user.first_name %>

Assuming you have belongs_to :user in Status model.

Upvotes: 2

Related Questions