简体   繁体   中英

How do I setup a "featured section", on my home page in my app, that will only be editable by an 'admin'?

So on the home#index of my app, the first row has 4 images side by side.

That is called the featured section. Those 4 images will be selected by the admin and they will be seen by everyone.

I don't have a home model, just a homeController .

So, given that I want to be able to store those images as separate objects and allow regular CRUD operations on those images that will be featured ONLY on the home#index and perhaps home#edit will be the edit page...how should I approach this?

Should I create a standalone model, called featured_images or something like that and then just pull from that model in the Home controller?

That's one approach I thought of, but I am wondering if it is overkill to create a model just for a handful of images? But then again, those images will be changing on a regular basis - so maybe they aren't just a 'handful'.

Thoughts?

"Given that you want to be able to store those images"... means it is not overkill to create a model. Rails is going to make it very easy for you to store the images as models and very difficult to store them any other way. Go with the easy way.

A Model object doesn't have to have an ActiveRecord (or other ORM) store backing it; it can use the filesystem as well. Or it can store no article other than the contained logic. The primary purpose of the Model is to encapsulate the business logic and abstract it away from the controller layer. This results in code that is easier to read, test, and maintain. Where the subject doesn't involve 'business' logic, a helper or library module might be a better fit.

If the logic is extremely simple you can certainly get away with keeping it in the controller. But before doing so, determine if you feel the simplicity is worth the tradeoff of easier readability, testability, and maintenance. The answer might just be 'yes', but that won't usually be the case.

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