Rails 3 渲染部分 nil 对象
我觉得我在这里做了一些愚蠢的事情。我正在制作一个简单的待办事项列表应用程序(我意识到已经有一百万个)。我有一个项目定义以及可以分配该项目中的任务的各种状态。
无论如何,我在访问该页面时收到以下错误消息:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为问题在于您在 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 tostatus
, 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 tos
via a local variable namedstatus
.Also, if simply
:status => s
does not work, try:locals => {:status => s}
. In any case, accessstatus
in the view, not@status
.最大的问题是
:status
是渲染的一个选项。它呈现 http 代码,如 200 或 :ok。应该有效的是:
并且不要使用
@
作为变量。The biggest problem is that
:status
is an option for render. It renders the http code like 200 or :ok.What should work is:
and don't use the
@
for the variable.