2016-03-02 77 views
0

我有一个索引页面,其中有很多详细信息,并且希望创建一个摘要索引页面;但它无法正确显示。某些类型的路由/范围问题我怀疑。替代索引页面上的错误

我创建了一个名为years/index-summary.html.erb的页面。在years/index.html.erb页面中使用的相同代码会导致错误。 yearslocationspeople之间的连接。

我加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)时使用了详细的索引页面,但对于公众来说,简化页面应该是默认页面,并且会有链接到详细(索引)页面,其中有两个其他长文本字段。

+1

你是否正确设置@years变量?,它可能只是在'索引'操作中设置它。 – fanta

+0

也许吧。但是,我需要定义什么操作?换句话说,我发现索引页面在控制器中调用了索引操作,但不知道在这种情况下该怎么做。但我会阅读指南中的控制器。新手。 – Greg

回答

0

@fanta。你让我思考。我发现行动是index-summary。我将新文件summary.html.erb重新命名,因为要避免与连字符混淆(方法名称带连字符不起作用,当我替换下划线时,它仍然不起作用,所以移动了)。相应地更改了路线,在years控制器中创建了一个动作summary,并从索引中复制,一切正常。我确信有更干净的方法可以做到这一点,但我已经开始运行。