0
我想顯示註釋對象的創建時間。我可以在沒有分組的情況下正確顯示它們,但是當我嘗試分組時,它似乎正在失去對對象屬性的引用。這是我得到的錯誤:爲什麼group_by方法改變我的對象的屬性?
undefined method `title' for #<Array:0x0000000aeb52d0>
在此行中:
<%= link_to note.title, {:action => 'show', :id => note.id} -%>
這裏是視圖的相關部分:
<ul id = "notes">
<% @notes.group_by(&:created_at).each do |note| %>
<li>
<%= link_to note.title, {:action => 'show', :id => note.id} -%>
<% if note.category == 0%>
<%= label_tag 'category', 'Note' %>
<% else %>
<%= label_tag 'category', 'Goal' %>
<%= note.dueDate %>
<% end %>
</li>
<% end %>
</ul>
這裏是遷移:
class Notes < ActiveRecord::Migration[5.1]
def change
create_table :notes do |t|
t.string :title, limit: 40, null: false
t.boolean :category, default: false, null: false
t.string :description, limit: 1000
t.string :dueDate
t.timestamps
end
end
end
只是對代碼的小意見。 Ruby on Rails有幾個與命名模型類和列有關的約定。模型類應以單數形式命名(例如Note),但表名應以複數形式(註釋)。列名應該寫入snake_case,例如due_date。 –