我試圖打印所有與清單相匹配的項目。清單中的NoMethodError#show,未定義的方法`each'for#<Item:0x007f9e441517d0>
我得到了清單#秀,未定義的方法`每個錯誤NoMethodError」的#Item:0x007f9e441517d0
不知道是什麼原因造成的,任何幫助,將不勝感激。
檢視: 應用/視圖/清單/ show.html.erb
<h1> <%= @checklist.name %> </h1>
<ul class="item">
<% @items.each do |item| %>
<li>
<%= item.item_name %>
<%= item.eta %>
<%= item.complete %>
</li>
<% end %>
</ul>
控制器: 應用程序/控制器/ checklists_controller.rb
def show
@checklist = Checklist.find(params[:id])
@items = Item.find_by(checklist_name: @checklist.name)
end
在show動作中將checklist.name更改爲checklist.id。您正在將checklist_id設置爲名稱。 –
這是故意的。 Checklist_id是一個字符串,應該與Checklist表中的名稱列相對應。我現在將列名checklist_id更正爲checklist_name。 – user8067471