Michele
Michele

Reputation: 95

NoMethodError in Users#show

Everytime I try and access the user profile page, I get this error.

ActionView::Template::Error (undefined method `email' for nil:NilClass):
2:   <tr>
3:     <td class="main">
4:       <h1>
5:         <%= gravatar_for @user %>
6:         <%= @user.name %>
7:       </h1>
8:     </td>

app/helpers/users_helper.rb:4:in `gravatar_for'
app/views/users/show.html.erb:5:in `_app_views_users_show_html_erb__222181804_33777120'

Below is what my users.controller.rb file looks like.

def show
@user = User.find(params[:id])
@microposts = @user.microposts.paginate(:page => params[:page])
@title = @user.name

For additional reference, below is a copy of my show.html.erb file.

<table class="profile" summary="Profile information">
  <tr>
    <td class="main">
      <h1>
        <%= gravatar_for @user %>
        <%= @user.name %>
      </h1>
    </td>
    <td class="sidebar round">
      <strong>Name</strong> <%= @user.name %><br />
      <strong>URL</strong>  <%= link_to user_path(@user), @user %>
    </td>
  </tr>
</table>

Also provided is my users_helper.rb file.

module UsersHelper

  def gravatar_for(user, options = { :size => 50 })
    gravatar_image_tag(user.email.downcase, :alt => h(user.name),
                                            :class => 'gravatar',
                                            :gravatar => options)
  end
end

Upvotes: 0

Views: 622

Answers (1)

Muhammad Sannan Khalid
Muhammad Sannan Khalid

Reputation: 3137

The error is in your helper method. Please verify that the @user object is not nil. Then in your view change the line number 5 like this:

<%= gravatar_for(@user) %>

define your users_helper#gravatar_for method like this

def gravatar_for(@user)
  ..........
  @user.email
  ..........
  rerurn somthing #if you want
end

Upvotes: 1

Related Questions