简体   繁体   English

具有主要类别的类别(Ruby on Rails)

[英]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:我宁愿:

  • a) create a new model, Pages (also with Action Text if you need). a) 创建一个新的 model,页面(如果需要,还可以使用操作文本)。 Maybe give each page a unique "key", which identifies where to load that Page model (eg 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-proof也许给每个页面一个唯一的“密钥”,它标识在哪里加载该页面 model (例如Page.find_by(key: "categories_top") )-> 如果您以后有需要 CMSify 的页面的不同部分,则该方法是相对面向未来的
  • b) But if the text does not change very often, I would just put the HTML into the view directly, b)但是如果文本不经常更改,我会直接将 HTML 放入视图中,
  • c) or into a separate Markdown file, if it is a lot of copy and you would rather not write HTML directly, and load that (using redcarpet Gem for instances) c) 或者到一个单独的 Markdown 文件中,如果它有很多副本并且你不想直接写 HTML,然后加载它(使用 redcarpet Gem 实例)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM