【发布时间】:2011-09-17 03:08:08
【问题描述】:
使用助手中的方法相应地隐藏或显示链接。方法 new_cate?按设计工作。但是方法link_to_edit?导致以下错误:
undefined local variable or method `cate' for #<#<Class:0x4ed6a10>:0x4ed2d38>
Extracted source (around line #23):
20: <tr>
21: <td><%= cate.name %></td>
22: <td><%= cate.description %></td>
23: <td><%= link_to_edit? %>></td>
24:
25: </tr>
26: <% end %>
代码如下:
在 index.html.erb 中
<body>
<h2>Category</h2>
<table>
<tr>
<th>Category</th>
<th>Description</th>
</tr>
<% @categories.each do |cate| %>
<tr>
<td><%= cate.name %></td>
<td><%= cate.description %></td>
<td><%= link_to_edit? %>></td>
</tr>
<% end %>
</table>
<%= new_cate? %>
</body>
在 categories_helper.rb 中
def new_cate?
if session[:eng_dh]
return link_to 'New Category', new_category_path
end
end
def link_to_edit?
if session[:eng_dh]
return link_to 'Edit', edit_category_path(cate)
end
end
“return”后面的代码看起来很奇怪。新品?按设计工作。但是link_to_edit?没有。
有什么想法吗?谢谢。
【问题讨论】:
标签: ruby-on-rails ruby model-view-controller scope