无法在视图Rails 4中显示我的类别和子类别 [英] Can't display my category and subcategory in the views Rails 4
问题描述
我的网站有类别和子类别,这里是我在我的seeds.rb,我创建一个主类别称为视频和动画和4个子类别,而不是我将子类别分配到主类别。 p>
@category = Category.create!(name:Video and animation)
[Intro ,动画和3D,编辑和后期制作,其他]
@subcategory = Subcategory.create!(name:name,category_id:@ category.id)
end
在我的rails控制台,我看到一切正常,添加的产品的category_id更改为一个数字(应为整数)。
问题:如何在我的视图中显示类别,所以当有人点击时,他会获得该类别的所有产品。
如何显示所有的子类别遵循同样的原则。
这是我试图加入我的意见
<%Category.all.each do | %>
<%= 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%>
奇怪的是,当我把我的产品控制器
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
它只显示所需的子类别我想,但主要类别保持空。
如果我把它改为我的控制器
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
主类别根据需要工作,但子类别保持为空。 / p>
注意:在这两种情况下,在任何人点击任何内容之前,我会看到正确的类别和子类别。
这是我在3个模型中的每一个
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
感谢您的时间。
b
$ b
@categories = Category.includes(:subcategories)
添加视图
<%@ 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%>
更改控制器中的索引方法
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
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.
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
这篇关于无法在视图Rails 4中显示我的类别和子类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!