形式化+嵌套类别

发布于 2024-09-05 12:05:05 字数 279 浏览 3 评论 0原文

我有一个文章模型和一个类别模型。类别充当树。

构建选择列表以允许管理员从选择列表中选择类别以便稍后将其与文章关联的最佳方法是什么?

semantic_form_for(@article) do |f|
  f.input :title, :as => :string
  f.input :content, :as => :text
  f.input :category, :collection => #what should go here ?
end

I have an article model and an category model. Category act as tree.

What is the best approch to build a select list to allow the administrator to select an category from a select list to associate it later with an article ?

semantic_form_for(@article) do |f|
  f.input :title, :as => :string
  f.input :content, :as => :text
  f.input :category, :collection => #what should go here ?
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自演自醉 2024-09-12 12:05:05

试试这个:

f.input :category, :as => :select, :collection => Category.all

或者你可以将它们指定为这样(来自文档):

f.input :category,  :as => :select, :collection => { @cat1.name => @cat1id, @cat2.name => @cat2.id }
f.input :category,  :as => :select, :collection => ["cat1", "cat2", "cat3", "cat4", "cat5"]

Try this:

f.input :category, :as => :select, :collection => Category.all

Alternatively you could specify them as this (from the docs):

f.input :category,  :as => :select, :collection => { @cat1.name => @cat1id, @cat2.name => @cat2.id }
f.input :category,  :as => :select, :collection => ["cat1", "cat2", "cat3", "cat4", "cat5"]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文