Reputation: 1349
I have two models CategoryFolder and Categories. CategoryFolder has many Categories, and Category belongs to CategoryFolder with foreign_id "parent_id"
For some reason, when I try to loop through the folders and list the categories within them, the view page displays the record hashes, and I don't know how to get rid of it:
Categories Controller
def index
@folders = current_account.category_folders.order("created_at ASC")
@categories = current_account.categories.where(parent_id: nil).order("created_at ASC")
# authorize! :read, Category
render :layout => "admin"
end
Categories View#index
<% @folders.each do |folder| %>
<%= folder.categories.order("title ASC").each do |category| %>
<%= render partial: 'table_list', locals: { category: category } %>
<% end %>
<% end %>
The Folders-Categories relationship has been causing a few errors lately, and I'm not sure whether the relations are set up weirdly. For example, even when I delete a category, the category title will still show under something like "@folder.categories.each ~ link_to title" and when I click the title it gives me an error page.
Category.rb
belongs_to :folder, class_name: "CategoryFolder", :foreign_key => "parent_id"
CategoryFolder.rb
has_many :categories, class_name: "Category", foreign_key: "parent_id"
Upvotes: 1
Views: 188
Reputation: 2121
<% @folders.each do |folder| %>
<%= folder.categories.order("title ASC").each do |category| %>
<%= render partial: 'table_list', locals: { category: category } %>
<% end %>
<% end %>
Your loop should not have an equal sign, that equal prints out the loop results.
<% @folders.each do |folder| %>
<% folder.categories.order("title ASC").each do |category| %>
<%= render partial: 'table_list', locals: { category: category } %>
<% end %>
<% end %>
The second line should have no sign
Upvotes: 3