这是干燥代码的好地方吗?
我正在开发的一个网站的所有者要求我将“关于我们”页面设置为可编辑(由她通过网络界面)。事实上,她总共有 5 页需要编辑——关于我们、服务条款等等。
在旧的实现中,当这些页面是静态视图文件时,我将所有 URL 编码到 routes.rb
scope :controller => :home do
get :about
get :terms
# etc ...
end
现在,这些不同的操作正在从数据库(或任何地方)获取数据,这似乎是标准的RESTful 方法可能是创建一个 Pages
资源并将所有视图合并到一个 show
操作中。
这感觉不太对劲。单个资源通常不会像“关于我们”页面那样硬连接到网站中 - 页面的内容可能会发生变化,但页面本身不会去任何地方,并且页脚中有指向它的链接,在我们的一些网站中具体来说
,从 PagesController 中分解出各个路由会引发以下问题:
- 我无法使用像
about_path
这样的命名路由助手 - 站点上永久页面的路由将存储在数据库,这意味着......
- 维护将可能会让人头疼,因为那不是正常的保留路线的地方。
因此,目前我认为最好的方法是将这些 URL 编码到 routes.rb
中,并具有单独的控制器操作,每个操作都会从数据库中获取自己的页面。
有人可以分享一些见解吗?您如何处理不完全静态但仍需要硬连接到站点的数据?
The owner of a site that I am working on has asked me to make the About Us page editable (by her, through a web interface). In fact, there are 5 pages in total that she wants to make editable - About Us, Terms of Service, and so on.
In the old implementation, when these pages were static view files, I had all the URLs coded into routes.rb
scope :controller => :home do
get :about
get :terms
# etc ...
end
Now that these different actions are fetching data from the DB (or wherever) it seems like the standard RESTful approach might be to make a Pages
resource and consolidate all the views into a show
action.
That doesn't feel quite right. Individual resources aren't usually hardwired into the site the way an About Us page is - the contents of the page might change, but the page itself isn't going anywhere, and there are links to it in the footer, in some of our emails, etc.
Specifically, factoring out the individual routes from the PagesController would raise the following problems:
- I couldn't used named route helpers like
about_path
- The routes for permanent pages on the site would be stored in the database, which means that...
- maintenance would probably be a headache, since that is not the normal place to keep routes.
So currently I think that the best approach is to leave these URLs coded into routes.rb
, and have separate controller actions, each of which would fetch its own page from the DB.
Can anyone share some insight? How do you deal with data that's not totally static but still needs to be hard-wired into the site?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您打算允许降价,我喜欢页面控制器和模型的想法。如果您的布局感觉所有 5 个页面都应该具有相似的感觉,那么我会使用一个模板,该模板填充用户生成的内容和适当的导航。
我的选择是设置路由、创建视图(包括路由)并使用用户生成的 Markdown 填充视图。
如果不了解更多有关您网站的信息,很难说,但我的偏好是不允许用户生成反映网站身份(关于、条款等)的页面,除非这是他们付费的目的。
If you are going to allow markdown, I like the idea of a Pages controller and model. If your layout feels like all 5 pages should have a similar feel, then I'd go with one template that populates with the user generated content and appropriate navigation.
My choice would be to set the routes, make the views (including routing), and populate the views with the user generated markdown.
Without knowing more about your site, it's hard to say, but my preference is not to allow users to generate pages that reflect the site identity (About, terms, etc.) unless that's what they are paying for.