Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Rails Routes and Resources A Route to a Read Action Path Helpers

undefined local variable or method `page' for #<#<Class:0x007ff2ba33ba30>:0x007ff2bc0c61e0> Did you mean? @pages

What is wrong with this code?

<h1>Blog Pages</h1>

<% @pages.each do |t| %>
    <p><%= link_to t.title, page_path(page) %></p>

    <% end %>
Rails.application.routes.draw do
  resources :posts
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  get '/pages', to: 'pages#index'
  get '/pages/:id', to: 'pages#show', as: 'page'
end

3 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

In page_path(page) you're using a page variable that isn't defined anywhere. Since you're iterating through a @pages collection, I assume you wanted to name each instance as page, but instead they are named t, so page_path(t) would probably work.

To make it more readable and more in-line with what is shown in the video, you probably want the code to look more like this:

<% @pages.each do |page| %>
  <p><%= link_to page.title, page_path(page) %></p>
<% end %>
Ari Misha
Ari Misha
19,323 Points

Hiya Rick! Can you post your controller code especially "show" controller? And hey while you're at it , please consider using markdown cheatsheet in order for other programmers to go through it without the code looking cluttery and messy.

~ Ari

Vladislav Trotsenko
Vladislav Trotsenko
9,291 Points

With your case:

<% @pages.each do |t| %>
  <p><%= link_to(t.title, t) %></p>
<% end %>