简体   繁体   中英

Categories with main categories (Ruby on Rails)

I'm trying to do the following:

I have categories model that has_many posts model.

On my categories , I use action_text for body text content.

My issue: I would like to create a main page that lists all categories and their posts on a view. 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).

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. 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.

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. Sounds like you like to make some kind of mini-CMS?

I would rather:

  • a) create a new model, Pages (also with Action Text if you need). 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
  • b) But if the text does not change very often, I would just put the HTML into the view directly,
  • 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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