Ruby Rails 分类通过 Simple_Form 个集合
Ruby on Rails Categorization Via Simple_Form collections
我所做的是否实用,或者是否有更简单的方法来实施此 category/sub-category 系统?
我的分类系统
我有一个应用程序 posts
table 将由 blog_categories
table 组织。此分类有主要类别和子类别。 blog_categories
和 parent_id
以及 NULL
是主要类别。子类别 link 通过 :string
与主要类别 :name
.
正在将类别分配给 post
我正在尝试使用 simple_form
实现此功能,并希望使用下拉选择器,根据主类别划分子类别集合。我 运行 遇到了在尝试制作数组数组时只显示每个数组中的第一项的问题:
<%= f.input :category_id, prompt: "Select Category", collection: [
["No-category"],
["All News","Audio Industry","Game Audio","Film Audio"],
["All Reviews","Software","Hardware"],
["All Interviews","Sound Designers","Game Developers","Voice Talent"],
["All Tutorials","Sound Design","Composition","Implementation","Voice Acting"]
], input_html: { class: "form-control center" } %>
blog_category 模态:
class BlogCategory < ApplicationRecord
has_many :posts
# This is called a self referential relation. This is where records in a table may point to other records in the same table.
has_many :sub_blog_categories, class_name: "Category", foreign_key: :parent_id
end
blog_categories Table:
t.string "name"
t.string "parent_id"
Post 模态: belongs_to :blog_category
posts Table: t.string "category_id"
是否有不同的方式在下拉列表中添加 groups/dividers?
我是否应该采用这种方法来分配子类别?
这在使用主类别和子类别时是否有效?
查看 simple_form 的文档,它可以满足您的需求:
https://github.com/plataformatec/simple_form#collections
f.input :country_id, collection: @continents, as: :grouped_select, group_method: :countries
这个正常的功能由HTMLselect提供,所以你走对了!
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/optgroup
在你的情况下,我相信这就是你想要的:
f.input :post_id, collection: @categories, as: :grouped_select, group_method: :posts
因为你要提供一个类别列表,它们有关联'posts',就是你要打的方法:@categories.first.posts
我找到了一个解决方案,通过使用以下 simple_form 输入,根据从 :name
中选择的 BlogCategory.id
分配 category_id
:
<%= f.input :category_id, collection: BlogCategory.all, label_method: :name, value_method: :id , input_html: { class: "form-control center" } %>
BlogCategory.all
提取所有已输入 blog_category
table 的类别。
label_method
拉取 blog_category
table 中每个项目的 :name
。
value_method
根据 blog_category
[=32] 中各个字段 :name
的 .id
为下拉菜单中的所有选择分配一个值=].
我所做的是否实用,或者是否有更简单的方法来实施此 category/sub-category 系统?
我的分类系统
我有一个应用程序 posts
table 将由 blog_categories
table 组织。此分类有主要类别和子类别。 blog_categories
和 parent_id
以及 NULL
是主要类别。子类别 link 通过 :string
与主要类别 :name
.
正在将类别分配给 post
我正在尝试使用 simple_form
实现此功能,并希望使用下拉选择器,根据主类别划分子类别集合。我 运行 遇到了在尝试制作数组数组时只显示每个数组中的第一项的问题:
<%= f.input :category_id, prompt: "Select Category", collection: [
["No-category"],
["All News","Audio Industry","Game Audio","Film Audio"],
["All Reviews","Software","Hardware"],
["All Interviews","Sound Designers","Game Developers","Voice Talent"],
["All Tutorials","Sound Design","Composition","Implementation","Voice Acting"]
], input_html: { class: "form-control center" } %>
blog_category 模态:
class BlogCategory < ApplicationRecord
has_many :posts
# This is called a self referential relation. This is where records in a table may point to other records in the same table.
has_many :sub_blog_categories, class_name: "Category", foreign_key: :parent_id
end
blog_categories Table:
t.string "name"
t.string "parent_id"
Post 模态: belongs_to :blog_category
posts Table: t.string "category_id"
是否有不同的方式在下拉列表中添加 groups/dividers?
我是否应该采用这种方法来分配子类别?
这在使用主类别和子类别时是否有效?
查看 simple_form 的文档,它可以满足您的需求: https://github.com/plataformatec/simple_form#collections
f.input :country_id, collection: @continents, as: :grouped_select, group_method: :countries
这个正常的功能由HTMLselect提供,所以你走对了! https://developer.mozilla.org/en-US/docs/Web/HTML/Element/optgroup
在你的情况下,我相信这就是你想要的:
f.input :post_id, collection: @categories, as: :grouped_select, group_method: :posts
因为你要提供一个类别列表,它们有关联'posts',就是你要打的方法:@categories.first.posts
我找到了一个解决方案,通过使用以下 simple_form 输入,根据从 :name
中选择的 BlogCategory.id
分配 category_id
:
<%= f.input :category_id, collection: BlogCategory.all, label_method: :name, value_method: :id , input_html: { class: "form-control center" } %>
BlogCategory.all
提取所有已输入 blog_category
table 的类别。
label_method
拉取 blog_category
table 中每个项目的 :name
。
value_method
根据 blog_category
[=32] 中各个字段 :name
的 .id
为下拉菜单中的所有选择分配一个值=].