半静态页面菜单部分
我创建了一个半静态页面,我想将其添加到 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" %>
Pages/_menu.html.erb
<% @pages.each do |page| %>
<li><%=link_to page.name, page.name %></li>
<% end %>
Rails -v 3.0.7
我已经尝试了一些方法,但这里仍然有问题,有一段时间没有在 Rails 项目上工作了,我很生疏!
预先感谢您的任何帮助!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@pages 未初始化。由于您在应用程序布局中访问它,我建议您在 application_controller.rb 中定义一个 before_filter 来初始化此 @pages 数组。
@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.