在 Rails 中创建管理目录
我已经为一个网站开发 CMS 后端几周了。这个想法是首先在后端制作所有内容,以便它可以管理将在主网站上显示的数据库和信息。
到目前为止,我所有的代码设置都在正常的 Rails MVC 结构中。所以用户管理是/users,视频是/videos。
我的计划是获取此代码并将其移动到 /admin 目录。因此,上面的两个控制器需要由 /admin/users 和 /admin/videos 访问。我不确定如何执行 ruote(添加 /admin 作为前缀),也不知道如何管理逻辑。我正在考虑做的是设置一个额外的“中间”控制器,当访问 /admin 目录时,它以某种方式嵌套在 ApplicationControler 和目标控制器之间。这样,任何额外的标志和重载方法都可以仅为 /admin 部分生成(我相信我也可以为此使用过滤器)。
如果这可行,那么下一个问题将是分离视图逻辑(但这只是重命名文件夹等)。
要么我这样做,要么我有两个在它们之间共享 MVC 代码的 Rails 实例(我猜数据库也是如此),但我担心这会导致大量重复错误。
关于我应该如何去做这件事有什么想法吗?
非常感谢!
I've been developing the CMS backend for a website for a few weeks now. The idea is to craft everything in the backend first so that it can manage the database and information that will be displayed on the main website.
As of now, I currently have all my code setup in the normal rails MVC structure. So the users admin is /users and videos is /videos.
My plans are to take the code for this and move it to a /admin directory. So the two controllers above would need to be accessed by /admin/users and /admin/videos. I'm not sure how todo the ruote (adding the /admin as a prefix) nor am I sure about how to manage the logic. What I'm thinking of doing is setting up an additional 'middle' controller that somehow gets nested between the ApplicationControler and the targetted controller when the /admin directory is accessed. This way, any additional flags and overloaded methods can be spawned for the /admin section only (I believe I could use a filter too for this).
If that were to work, then the next issue would be separating the views logic (but that would just be renaming folders and so on).
Either I do it that way or I have two rails instances that share the MVC code between them (and I guess the database too), but I fear that would cause lots of duplication errors.
Any ideas as to how I should go about doing this?
Many thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不介意每个资源有两个控制器,则可以有一个单独的“admin”命名空间。我喜欢这种方式,因为管理部分与公共部分完全不同。管理控制器实现所有 CRUD 操作,而公共控制器仅实现显示和索引操作。
routes.rb:
您的控制器可能类似于:
命名空间控制器和视图在 app/controllers 和 app/views 目录中拥有自己的子目录。如果您使用
form_for
帮助器,则需要修改其参数:If you don't mind having two controllers for each resource, you could have a separate "admin" namespace. I like it this way, since the admin section is completely different from the public one. Admin controllers implement all CRUD actions, whereas the public ones implement only show and index actions.
routes.rb:
Your controllers could be something like:
Namespaced controllers and views have their own subdirectory inside the app/controllers and app/views directories. If you use the
form_for
helper, you need to modify its parameters:您可以在没有额外控制器的情况下完成此操作,在
config/routes.rb
中相对容易::requirements
在这里实际上做了什么,因为我给了它一个常量而不是正则表达式,只需在通过此路由访问时添加params[:is_admin]
即可。因此,您可以在控制器中检查此值,并渲染不同的视图,或者如果两个视图相似,您可以在视图中检查它。在非管理版本上包含false
的要求非常重要,否则人们只能使用/users/?is_admin=true
。:name_prefix
编辑路由名称,例如admin_video_path(123)
以及video_path(123)
。在 Rails 2.3.5 上测试,其他版本可能有所不同。有关 RESTful 路由上可用选项的更多信息,请参阅 ActionController::Resources 文档。
You can do this without an extra controller, relatively easily in
config/routes.rb
:What
:requirements
actually does here, because I gave it a constant and not a regex, is just to addparams[:is_admin]
when accessed via this route. So you can check this value in your controller, and render different views, or you can just check it in the view if the two views are similar. It's important to include the requirement withfalse
on the non-admin versions otherwise people can just use/users/?is_admin=true
.The
:name_prefix
edits the route names, so you have e.g.admin_video_path(123)
as well asvideo_path(123)
.Tested on Rails 2.3.5, other versions may differ. For more about the options available on RESTful routes, see the ActionController::Resources docs.