Rails 列表问题

发布于 2024-11-07 07:39:48 字数 774 浏览 0 评论 0原文

我生成了一个脚手架并创建了一个临时应用程序。我想在侧边栏上列出应用程序中各处的脚手架项目(在 application.html.erb 内)。 我的左栏在 application.html.erb 中看起来像这样:

>  <div id="leftcolumn">
>     <% @items.each do |link| %>
>       <%= link_to link.title, link %>
>     <% end %>
>  </div>

单击每个项目将导致它的 Show 方法。 运行后,每当我尝试访问任何项目的任何方法时,它都会显示以下错误:

>     NoMethodError in Items#show
>     
>     Showing F:/Rails/items-list/app/views/layouts/application.html.erb
>     where line #17 raised:
>     
>     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

并且后面的代码是左列的代码。 我该如何解决这个问题?

I generated a scaffold and I created a casual application. I want to list the scaffold items on a sidebar, everywhere on the application (inside application.html.erb).
My left column, in application.html.erb looks like that:

>  <div id="leftcolumn">
>     <% @items.each do |link| %>
>       <%= link_to link.title, link %>
>     <% end %>
>  </div>

Clicking on each item would lead to it's Show method.
After running that, whenever I try to access any method of any item, it shows me the following error:

>     NoMethodError in Items#show
>     
>     Showing F:/Rails/items-list/app/views/layouts/application.html.erb
>     where line #17 raised:
>     
>     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

And the followed code is the left column's code.
How can I fix that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

他是夢罘是命 2024-11-14 07:39:48

Rails 正在抱怨,因为你试图将 nil 视为数组。如果没有发布 application.html.erb 的行号,我必须猜测,但我猜测 @items 为零。

如果您将其包含在布局中,则需要确保在每个请求上都设置了 @items 数组。是吗?一种简单的方法是在 application_controller.rb 中放置一个 before_filter

class ApplicationController < ActionController::Base
    ...
    before_filter setup_items
    ...
    def setup_items
        @items = Item.all
    end
    ...
end

这假设所有使用此布局的控制器都扩展了应用程序控制器。

Rails is complaining because you're trying to treat nil as an array. Without posting the line numbers for application.html.erb, I have to guess, but I'd guess that @items is nil.

If you're including this in your layout, then you need to make sure that your @items array is set on every request. Is it? One easy way of doing this would be to put a before_filter in your application_controller.rb.

class ApplicationController < ActionController::Base
    ...
    before_filter setup_items
    ...
    def setup_items
        @items = Item.all
    end
    ...
end

This assumes that all controllers using this layout extend the application controller.

饭团 2024-11-14 07:39:48

确保您的控制器具有以下内容:

@items = Item.all

然后在您的视图中您需要以下内容:

<div id="leftcolumn">
   <% @items.each do |link| %>
     <%= link_to link.title, link if link.title%>
   <% end %>
</div>

它可能会失败,因为其中一个链接没有标题。

Make sure your controller has this:

@items = Item.all

Then in your view you want this :

<div id="leftcolumn">
   <% @items.each do |link| %>
     <%= link_to link.title, link if link.title%>
   <% end %>
</div>

It could be failing because one of the links does not have a title.

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