半静态页面菜单部分

发布于 2024-11-24 00:49:55 字数 1193 浏览 2 评论 0原文

我创建了一个半静态页面,我想将其添加到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

带刺的爱情 2024-12-01 00:49:56

@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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文