Getting undefined method error after going to home page

145 Views Asked by At

I'm building a Rails app that displays a user's gravatar in the header of the site. The helper method is as follows:

app/helpers/users_helper.rb

# Returns the Gravatar for the given user.
def gravatar_for(user)
  gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
  gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}"
  image_tag(gravatar_url, alt: user.name, class: "gravatar")
end

The problem I'm having is that when the logged-in user clicks the logo link to go to the homepage, I'm getting this error:

undefined method `email' for nil:NilClass

I'm using an instance variable to display the user's gravatar in the header. This is the code that's in the navigation menu:

app/views/layouts/_header.html.erb

<header class="site-header">
<div class="site-header-content">
    <div class="site-header-content__nav">
        <figure class="site-header-content__nav--logo">
            <%= link_to image_tag("global/logo.png", alt: "Lemur") %>
            <figcaption><%= link_to "Lemur",   root_path %></figcaption>
        </figure>
        <input type="checkbox" id="toggle" />
        <nav class="site-header-content__nav--menu">
            <label for="toggle" class="toggle" data-open="&#xf0c9;" data-close="&#xf00d;" onclick></label>
            <ul>
                <% if logged_in? %>
                    <li><%= link_to "Users", users_path %></li>
                    <li><%= link_to "Sign Out", logout_path, method: "delete" %></li>
                <% else %>
                    <li><%= link_to "About",   about_path %></li>
                    <li><%= link_to "Terms",   terms_path %></li>
                    <li><%= link_to "Help",   help_path %></li>
                    <li><%= link_to "Sign In", login_path %></li>
                <% end %>
            </ul>
        </nav>
    </div>
    <% if current_page?(root_path) && !logged_in? %>
        <div class="site-header-content__callout">
            <h1>Get the word out!</h1>
            <p>Write about what's happening around you, post about upcoming events and more with Lemur.</p>
            <div class="site-header-content__callout--buttons">
                <%= link_to "Sign Up", signup_path, class: "btn btn__lg btn__primary" %>
                <%= link_to "Sign In", login_path, class: "btn btn__lg btn__primary" %>
            </div>
        </div>
        <div class="site-header-content__down-arrow">
            <a href="#home-feature__local"><%= embedded_svg "down.svg" %></a>
        </div>
    <% end %>
</div>

What am I doing wrong here? I want to have the gravatar appear site wide when the user is still logged in. Thanks in advance.

1

There are 1 best solutions below

2
On

You mentioned like " when the logged-in user clicks the logo link to go to the homepage, I'm getting this error:"

So check your root_path. in config/routes.rb (I assume by clicking on logo, You are taking the user to home page.)

Eg: root 'home#index'

Then in HomeController

def index
  @user = get the user
end