[英]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.所以在我的应用程序的
home#index
上,第一行有 4 张图像并排。
That is called the featured
section.这就是所谓的
featured
部分。 Those 4 images will be selected by the admin
and they will be seen by everyone.这 4 张图片将由
admin
选择,每个人都可以看到。
I don't have a home
model, just a homeController
.我没有
home
模型,只有一个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?因此,鉴于我希望能够将这些图像存储为单独的对象,并允许对那些仅在
home#index
上显示的图像进行常规 CRUD 操作,也许home#edit
将是编辑页面......我应该如何接近这个?
Should I create a standalone model, called featured_images
or something like that and then just pull from that model in the Home
controller?我应该创建一个独立的模型,称为
featured_images
或类似的东西,然后在Home
控制器中从该模型中提取吗?
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.
Rails 将使您很容易将图像存储为模型,而很难以其他方式存储它们。 Go with the easy way.
走简单的路。
A Model object doesn't have to have an ActiveRecord (or other ORM) store backing it;模型对象不必有一个 ActiveRecord(或其他 ORM)存储来支持它; 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.
答案可能只是“是”,但通常情况并非如此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.