Rails -v 3.2.3
我试图在我的视图中显示表格内的链接
我一直在
NoMethodError in Links#submissions , views/links/submissions.html.erb where line #10 raised: undefined method "each" for nil:NilClass
继承人 submission.htm.erb
<table>
<% @links.each do |link| %>
<tr valign = "top">
<td>
<%= link_to_remote "Up",
:url => { :action => 'modify_points',
:id => link.id,
:by => 1 },
:update => "link" + link.id.to_s %>
</td>
<td>
<%= link_to_remote "Down",
:url => { :action => 'modify_points',
:id => link.id,
:by => -1 },
:update => "link" + link.id.to_s %>
</td>
<td>
<b> <a href="<%= link.url %>"> <%= link.description %> </a> </b>
(<%= if link.url =~ /(:\/\/) ([^\/]*)/ then $2 else "wrong URL" end %>) <br/>
<span id="link"><%= link.id.to_s %>"%><%= link.points %> </span>
points posted <%= time_ago_in_words link.created_at %> ago
</td>
</tr>
<% end %>
</table>
我的问题是......我是否需要在没有链接的情况下编写一些代码?这是我收到此错误消息的原因吗?
答案 0 :(得分:1)
是的,如果你不知道@links是否会在上下文中存在,那么比对不起更安全。
<% if @links.present? %>
<% @links.each do |link|
etc
<% end %>
<% end %>
等等......也许这里有一些更好的红宝石简写..如果我确信有人会提醒我们。 :)