Rails 3 渲染部分 nil 对象

发布于 2024-11-27 19:06:25 字数 1490 浏览 1 评论 0原文

我觉得我在这里做了一些愚蠢的事情。我正在制作一个简单的待办事项列表应用程序(我意识到已经有一百万个)。我有一个项目定义以及可以分配该项目中的任务的各种状态。

无论如何,我在访问该页面时收到以下错误消息:

nil:NilClass 的未定义方法“title”

这个关于 nil 对象的问题 非常相似,但是,建议的解决方案似乎无法解决我遇到的问题。我只有一种与该项目相关的状态,而且它不是零。我想知道这个问题是否与状态关联有关...

在我的项目中,我有:

    <% @project.statuses.each do |s| %> 
      <%= s.inspect %> 
      <%= render 'statuses/show', :status => s %>
   <% end %>

#if I take out the render line - the status shows up 

现在的状态基本上只是一个通用的脚手架视图 - 如果我直接从控制器传递数据将收到一个对象@status。 ... 我已经尝试使用 render :partial..., :locals =>; { :status = s} 等等。如有任何建议,我们将不胜感激。

更新 - 添加_show Partial:

<p id="notice"><%= notice %></p>

<p>
  <b>Title:</b>
  <%= @status.title %>
</p>

<p>
  <b>Description:</b>
  <%= @status.description %>
</p>

<p>
  <b>Active:</b>
  <%= @status.active %>
</p>

<%= link_to 'Edit', edit_status_path(@status) %> |
<%= link_to 'Back', statuses_path %>

更新 - 添加更多错误消息

NoMethodError in Projects#show

Showing /home/.../app/views/statuses/_show.html.erb where line #5 raised:

undefined method `title' for nil:NilClass

I feel like I'm doing something dumb here. I'm making a simple TODO list app (I realize there are already a million of them). I have a project definition and the various statuses that a task within in that project can be assigned.

No matter what, I'm receiving the following error message when hitting the page:

undefined method `title' for nil:NilClass

This question about nil objects is extremely similar, however, the suggested solutions don't seem to fix the issue I'm encountering. I only have one status associated with the project and it is not nil. I'm wondering if this issue has anything to do with the statuses being an association...

In my project I have:

    <% @project.statuses.each do |s| %> 
      <%= s.inspect %> 
      <%= render 'statuses/show', :status => s %>
   <% end %>

#if I take out the render line - the status shows up 

The status right now is basically just a generic scaffolded view -- if I was passing the data from a controller directly it would receive an object @status.
...
I have tried this with render :partial..., :locals => { :status = s} etc. etc. Any suggestions would be appreciated.

Update - Added _show Partial:

<p id="notice"><%= notice %></p>

<p>
  <b>Title:</b>
  <%= @status.title %>
</p>

<p>
  <b>Description:</b>
  <%= @status.description %>
</p>

<p>
  <b>Active:</b>
  <%= @status.active %>
</p>

<%= link_to 'Edit', edit_status_path(@status) %> |
<%= link_to 'Back', statuses_path %>

Update - Added more of the error message

NoMethodError in Projects#show

Showing /home/.../app/views/statuses/_show.html.erb where line #5 raised:

undefined method `title' for nil:NilClass

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

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

发布评论

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

评论(2

捂风挽笑 2024-12-04 19:06:25

我认为问题在于您在 ERB 部分中引用了 @status ,而您应该引用 status ,而没有前导 @.

@status 表示查找已定义的实例变量。传入 :status => 时s 意味着视图可以通过名为 status局部变量访问s

另外,如果只是 :status =>; s 不起作用,请尝试 :locals => {:状态=> s}。无论如何,访问视图中的 status,而不是 @status

I believe the problem is that you're referring to @status in the ERB partial, where as you should be referring to status, without the leading @.

The @status means to look for a defined instance variable. While passing in the :status => s means that the view will have access to s via a local variable named status.

Also, if simply :status => s does not work, try :locals => {:status => s}. In any case, access status in the view, not @status.

三生一梦 2024-12-04 19:06:25

最大的问题是 :status 是渲染的一个选项。它呈现 http 代码,如 200 或 :ok。

应该有效的是:

<%= render :partial => 'statuses/show', :locals => { :status => s } %>

并且不要使用 @ 作为变量。

<%= status.title %>

The biggest problem is that :status is an option for render. It renders the http code like 200 or :ok.

What should work is:

<%= render :partial => 'statuses/show', :locals => { :status => s } %>

and don't use the @ for the variable.

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