[英]Categories with main categories (Ruby on Rails)
I'm trying to do the following:我正在尝试执行以下操作:
I have categories
model that has_many posts
model.我有
categories
model有很多posts
model。
On my categories
, I use action_text
for body text content.在我的
categories
中,我使用action_text
作为正文内容。
My issue: I would like to create a main page that lists all categories
and their posts
on a view.我的问题:我想创建一个主页,在视图中列出所有
categories
及其posts
。 And jus like a regular category, I want that page to have an action_text
field for me to populate it with body text content (saved in database of course).就像一个常规类别一样,我希望该页面有一个
action_text
字段供我使用正文文本内容(当然保存在数据库中)填充它。
Then I want my routes to be as such:然后我希望我的路线是这样的:
match '/all' => 'pages#all_content', :via => :get, :as => :all_content
match '/:category_id' => 'pages#category', :via => :get, :as => :category
match '/:category_id/:post_id' => 'pages#post', :via => :get, :as => :post
My unsure solution: Add a boolean main
field to my category
model.我不确定的解决方案:将 boolean
main
字段添加到我的category
model。 That allows me to be able to populate it with content just like a regular category.这使我能够像常规类别一样使用内容填充它。 And then, on my
pages#all_content
, I can request the main category to have all its data.然后,在我的
pages#all_content
上,我可以请求主类别拥有其所有数据。
But I'm very insecure about this solution because I think there must be a better/cleaner one.但是我对这个解决方案非常不安全,因为我认为必须有一个更好/更清洁的解决方案。 Am I wrong?
我错了吗?
I wouldn't mix those things.我不会混合这些东西。 Using main to get one category just for the ActionText Content sounds more like a Hack.
使用 main 为 ActionText 内容获取一个类别听起来更像是一个 Hack。 Sounds like you like to make some kind of mini-CMS?
听起来你喜欢制作某种迷你 CMS?
I would rather:我宁愿:
Page.find_by(key: "categories_top")
) -> If you later have different parts of the page which you need to CMSify, that approach is the relatively future-proofPage.find_by(key: "categories_top")
)-> 如果您以后有需要 CMSify 的页面的不同部分,则该方法是相对面向未来的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.