Search code examples
mysqlruby-on-railsubuntupassengerproduction

NameError in StaticPages#home


I'm getting the "NameError" after doing the following in routes.rb

Rails.application.routes.draw do
  root 'static_pages#home'
  get '/help',    to: 'static_pages/help'
  get '/about',   to: 'static_pages/about'
  get '/contact', to: 'static_pages/contact'
  get '/sigunup', to: 'users#new'

end

Can someone help me, please? Thanks.

Showing /home/bdme551/bdme21/app/views/layouts/_header.html.erb where line #7 raised:

undefined local variable or method `help_path' for #<#<Class:0x007f7474f3d208>:0x007f747555c508>
Extracted source (around line #7):


      <ul class="nav navbar-nav navbar-right">
        <li><%= link_to "Home",   root_path %></li>
        <li><%= link_to "Help",   help_path %></li>
        <li><%= link_to "Log in", '#' %></li>
      </ul>
    </nav>

StaticPagesController has the following:

class StaticPagesController < ApplicationController
  def home
  end
  def help
  end
  def about
  end
  def contact
  end
end

Solution

  • The problem is you haven't defined a name for your custom GET route. If you run rake routes, you should not find a prefix for any route except root.

    You can add a name to a custom route with the as option.

    get '/help',    to: 'static_pages#help', as: :help
    

    Note that in your code you have static_pages/help as the value for to option.

    It should be static_pages#help. # refers to instance method.

    Now, if you try to run rake routes, you should find help as a prefix for /help route.

    Also, fix the other route definitions by replacing / with #.