半静态页面菜单部分

时间:2011-07-14 14:03:04

标签: ruby-on-rails partial-views static-pages

我创建了一个半静态页面,我想将其添加到application.html.erb文件中的菜单中,并通过部分调用所有视图,但它只能在页面中使用。

用户索引错误

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" %>

页/ _menu.html.erb

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

Rails -v 3.0.7

我已经尝试过一些东西,但是这里仍然存在问题,一段时间没有在rails项目上工作,我生锈了!

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

@pages未初始化。由于您是在应用程序布局中访问它,我建议您在application_controller.rb中定义一个before_filter来初始化此@pages数组。