Carlos
Carlos

Reputation: 1

NameError in Home#show in ruby

I have created a small Rails project called Bloccit. It contains an About section. Inside the About section I have defined an action.

<h1>About Bloccit</h1>
<p>Created by: <%= my_name %></p>

Also on the application.html.erb section

<!DOCTYPE html>
<html>
<head>
  <title>Bloccit</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>
</head>
<body>
    <div class="container">
        <ul class="nav nav-tabs">
            <li><%= link_to "Bloccit", root_path %></li>
            <li><%= link_to "About", about_path %></li>
        </ul>

    <%= yield %>
</div>

</body>
</html>

I have defined the About using the <li><%= link_to "About", about_path %></li> markup. In my routes.rb, I have

 Rails.application.routes.draw do

 resources :posts

 get "welcome/index"

  get 'about' => 'welcome#about'

  root to: 'welcome#index'


  resources :advertisements

end

After I rake the routes I see:

    Prefix Verb   URI Pattern                        Controller#Action
             posts GET    /posts(.:format)                   posts#index
                   POST   /posts(.:format)                   posts#create
          new_post GET    /posts/new(.:format)               posts#new
         edit_post GET    /posts/:id/edit(.:format)          posts#edit
              post GET    /posts/:id(.:format)               posts#show
                   PATCH  /posts/:id(.:format)               posts#update
                   PUT    /posts/:id(.:format)               posts#update
                   DELETE /posts/:id(.:format)               posts#destroy
             about GET    /about(.:format)                   welcome#about
              root GET    /                                  welcome#index
    advertisements GET    /advertisements(.:format)          advertisements#index
                   POST   /advertisements(.:format)          advertisements#create
 new_advertisement GET    /advertisements/new(.:format)      advertisements#new
edit_advertisement GET    /advertisements/:id/edit(.:format) advertisements#edit
     advertisement GET    /advertisements/:id(.:format)      advertisements#show
                   PATCH  /advertisements/:id(.:format)      advertisements#update
                   PUT    /advertisements/:id(.:format)      advertisements#update
                   DELETE /advertisements/:id(.:format)      advertisements#destroy

When I try to run this application in the browser it gives me a routing error. This is the errors I receive in the console:

Processing by HomesController#show as HTML
  Rendered homes/show.html.erb within layouts/application (0.1ms)
Completed 500 Internal Server Error in 107ms (ActiveRecord: 0.0ms)

ActionView::Template::Error (undefined local variable or method `about_path' for #<#<Class:0x007f80e226d9e0>:0x007f80e64e42b0>):
    10: <body>
    11: 
    12:         <li><%= link_to "Bloccit", root_path %></li>
    13:         <li><%= link_to "About", about_path %></li>
    14:     </ul>
    15: 
    16: <%= yield %>
  app/views/layouts/application.html.erb:13:in `_app_views_layouts_application_html_erb__31655232790982_70095796178040'


  Rendered /Users/carlos/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (7.7ms)
  Rendered /Users/carlos/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (4.1ms)
  Rendered /Users/carlos/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.0ms)
  Rendered /Users/carlos/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (98.8ms)
  Rendered /Users/carlos/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.1.3/lib/web_console/templates/_markup.html.erb (0.9ms)
  Rendered /Users/carlos/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.1.3/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.5ms)
  Rendered /Users/carlos/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.1.3/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.4ms)
  Rendered /Users/carlos/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.1.3/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.5ms)
  Rendered /Users/carlos/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.1.3/lib/web_console/templates/console.js.erb within layouts/javascript (102.1ms)
  Rendered /Users/carlos/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.1.3/lib/web_console/templates/main.js.erb within layouts/javascript (0.6ms)
  Rendered /Users/carlos/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.1.3/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.8ms)
  Rendered /Users/carlos/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.1.3/lib/web_console/templates/index.html.erb (219.3ms)

I am using this url to type in the browser: http://localhost:3000/

Thanks, Carlos

Upvotes: 0

Views: 103

Answers (1)

Toby
Toby

Reputation: 8792

In your config/routes.rb file I would consider making this addition;

get 'about' => 'welcome#about', :as => 'about'

It should then let you reference about_path

Upvotes: 1

Related Questions