我有一個索引頁面,其中有很多詳細信息,並且希望創建一個摘要索引頁面;但它無法正確顯示。某些類型的路由/範圍問題我懷疑。替代索引頁面上的錯誤
我創建了一個名爲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