我有一个索引页面,其中有很多详细信息,并且希望创建一个摘要索引页面;但它无法正确显示。某些类型的路由/范围问题我怀疑。替代索引页面上的错误
我创建了一个名为years/index-summary.html.erb
的页面。在years/index.html.erb
页面中使用的相同代码会导致错误。 years
是locations
和people
之间的连接。
我加get 'summary' => 'years#index-summary'
到routes.rb.
,在index
工作,但不index-summary
<table class="table table-condensed table-striped">
<tr><td> <%= render 'newcon' %></td></tr>
<% @years.each do |year| %>
<tr>
<td class="active"></td>
<td class="active"> On <%= year.year_date.strftime('%d' ' ' '%b' ' ' '%Y') %>, <strong> <%= year.person.try(:full_name) %> </strong> was a </td>
<td class="active"> <strong><%= year.title %> </strong> at</td>
<td class="active"> <strong> <%= year.location.try(:address) %>, <%= year.location.try(:city) %> </strong> which was a </td>
<td class="active"> <strong> <%= year.resto ? 'restaurant' : '' %> <%= year.resto_name? ? " named \"#{year.resto_name}\". " : '' %> <%= year.resid ? 'Residence' : '' %> </strong> </td>
<% end %>
</tr>
</table>
的错误是
Project/crores5/app/views/years/index-summary.html.erb where line #9 raised:
undefined method `each' for nil:NilClass
Extracted source (around line #9 [the last line in this code segment]):
<table class="table table-condensed table-striped">
<tr><td> <%= render 'newcon' %></td></tr>
<% @years.each do |year| %>
正如我说,如果我粘贴代码,它工作正常的代码到index.html.erb并转到`/ years'。使用Puma在Mac上以开发模式运行。
谢谢你的帮助。 Rails新手。
https://bitbucket.org/MtnBiker/crores5
或者,也许有更好的方式来处理这方面的需求。我在输入数据到数据库(通过Rails)时使用了详细的索引页面,但对于公众来说,简化页面应该是默认页面,并且会有链接到详细(索引)页面,其中有两个其他长文本字段。
你是否正确设置@years变量?,它可能只是在'索引'操作中设置它。 – fanta
也许吧。但是,我需要定义什么操作?换句话说,我发现索引页面在控制器中调用了索引操作,但不知道在这种情况下该怎么做。但我会阅读指南中的控制器。新手。 – Greg