2016-03-02 72 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,並從索引中複製,一切正常。我確信有更乾淨的方法可以做到這一點,但我已經開始運行。