0

I've created a semi static pages which I would like to add to a menu in my application.html.erb file, and call via a partial to all view but it only works in the page.

Error on user index

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
Extracted source (around line #1):

1: <% @pages.each do |page| %>
2: <li><%=link_to page.name, page.name %></li>
3: <% end %>

Routes.rb

Vgrant::Application.routes.draw do
  resources :pages

  get "user_sessions/new"

  root :to => "Pages#index"

  resources :users

  resources :user_sessions

  match 'login' => "user_sessions#new",      :as => :login
  match 'logout' => "user_sessions#destroy", :as => :logout

  match ':name', :controller => 'pages', :action => 'show'

end

application.html.erb

<%= render :partial => "pages/menu" %>

pages/_menu.html.erb

<% @pages.each do |page| %>
<li><%=link_to page.name, page.name %></li>
<% end %>

Rails -v 3.0.7

I've tried a few things but still have problems here, not worked on a rails project for a while am very rusty!

Thanks in advance for any help!

MrThomas
  • 427
  • 1
  • 6
  • 19

1 Answers1

2

@pages is not initialized. Since you're accessing it in application layout, I would suggest you define a before_filter in application_controller.rb to initialize this @pages array.

eugen
  • 8,916
  • 11
  • 57
  • 65