Can't display my category and subcategory in t

2019-08-01 06:53发布

问题:

My website has Categories and subcategories,here is what i have in my seeds.rb,there i create a main category called "Video and animation",and 4 subcategories,than i assign the subcategories to the main category.

@category = Category.create!(name:  "Video and animation")

["Intro", "Animation & 3D", "Editing and Post Production", "Other"].each do |name|

@subcategory = Subcategory.create!(name: name, category_id: @category.id)

end

It works well,in my rails console i see that everything works,the category_id for the added products changes to a number(integer as it should).

Question: How do i show the category in my views,so when someone clicks on that he gets all the products of that category. And how do i show all the subcategories following the same principle. Here is what i tried to put into my views

<% Category.all.each do |category| %>
  <%= link_to category.name, gigs_path(category: category.name) %>
<% end %>
<% Subcategory.all.each do |subcategory| %>
  <%= link_to subcategory.name, gigs_path(subcategory: subcategory.name) %>
<% end %>

The weird thing is that when i put in my product controller this

def index
      @category_id = Category.find_by(name: params[:category])
      @gigs = Gig.where(category_id: @category_id).order("created_at DESC")
      @subcategory_id = Subcategory.find_by(name: params[:subcategory])
      @gigs = Gig.where(subcategory_id: @subcategory_id).order("created_at DESC")
  end 

It shows just the required subcategories,as i want,but the main category remains empty. and if i put instead this into my controller

def index
      @category_id = Category.find_by(name: params[:category])
      @subcategory_id = Subcategory.find_by(name: params[:subcategory])
      @gigs = Gig.where(category_id: @category_id).order("created_at DESC")
  end

The main category works,as required,but the subcategories remain empty.

Note: In both cases,in the views,before anyone clicks on anything,i see the proper category and subcategories displayed.

Here is what i have in each of the 3 models

class Category < ActiveRecord::Base
  has_many :subcategories
end

class Subcategory < ActiveRecord::Base
  belongs_to :category
  has_many :products
end

class Product < ActiveRecord::Base
  acts_as_votable
  belongs_to :user
  belongs_to :subcategory
  has_attached_file :image, :styles => { :medium => "300x300>" }
  validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end

Thank you for your time.

回答1:

In controller action

@categories = Category.includes(:subcategories)

Add In views

<% @categories.each do |category| %>
     <%= link_to category.name, gigs_path(category: category.name) %>
     <% category.subcategories.each do |subcategory| %>
          <%= link_to subcategory.name, gigs_path(subcategory: subcategory.name) %>
     <% end %>
<% end %>

Change your index method in controller

def index
    if params[:category]
        c_id = Category.find(name: params[:category])
        @gigs = Gig.where(category_id: c_id).order("created_at DESC")
    elsif params[:subcategory]    
        subc_id = Subcategory.find(name: params[:subcategory])
        @gigs = Gig.where(subcategory_id: subc_id).order("created_at DESC")
    end    
end