在 Rails 中创建管理目录

发布于 2024-09-03 14:52:54 字数 547 浏览 4 评论 0原文

我已经为一个网站开发 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长安忆 2024-09-10 14:52:54

如果您不介意每个资源有两个控制器,则可以有一个单独的“admin”命名空间。我喜欢这种方式,因为管理部分与公共部分完全不同。管理控制器实现所有 CRUD 操作,而公共控制器仅实现显示和索引操作。

routes.rb:

map.namespace :admin do |admin|
  admin.resources :users
  admin.resources :videos
end

map.resources :videos, :only => [:index, :show]

您的控制器可能类似于:

class VideosController < PublicController; end

class Admin::VideosController < Admin::AdminController; end

class PublicController < ApplicationController
  layout 'public'
  before_filter :load_public_menu
end

class Admin::AdminController < ApplicationController
  layout 'admin'
  before_filter :login_required, :load_admin_menu
end

命名空间控制器和视图在 app/controllers 和 app/views 目录中拥有自己的子目录。如果您使用form_for帮助器,则需要修改其参数:

form_for [:admin, @video] do |f|

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:

map.namespace :admin do |admin|
  admin.resources :users
  admin.resources :videos
end

map.resources :videos, :only => [:index, :show]

Your controllers could be something like:

class VideosController < PublicController; end

class Admin::VideosController < Admin::AdminController; end

class PublicController < ApplicationController
  layout 'public'
  before_filter :load_public_menu
end

class Admin::AdminController < ApplicationController
  layout 'admin'
  before_filter :login_required, :load_admin_menu
end

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:

form_for [:admin, @video] do |f|
尹雨沫 2024-09-10 14:52:54

您可以在没有额外控制器的情况下完成此操作,在 config/routes.rb 中相对容易:

# non-admin routes
# your args could include :only => [:index,:show] for the non-admin routes
# if you wanted these to be read-only
map.resources :users,  ...your args..., :requirements => { :is_admin => false }
map.resources :videos, ...your args..., :requirements => { :is_admin => false }
# admin routes
map.resources :users,  ...your args..., :path_prefix => '/admin', \
    :name_prefix => 'admin_', :requirements => { :is_admin => true }
map.resources :videos, ...your args..., :path_prefix => '/admin', \
    :name_prefix => 'admin_', :requirements => { :is_admin => true }

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

# non-admin routes
# your args could include :only => [:index,:show] for the non-admin routes
# if you wanted these to be read-only
map.resources :users,  ...your args..., :requirements => { :is_admin => false }
map.resources :videos, ...your args..., :requirements => { :is_admin => false }
# admin routes
map.resources :users,  ...your args..., :path_prefix => '/admin', \
    :name_prefix => 'admin_', :requirements => { :is_admin => true }
map.resources :videos, ...your args..., :path_prefix => '/admin', \
    :name_prefix => 'admin_', :requirements => { :is_admin => true }

What :requirements actually does here, because I gave it a constant and not a regex, is just to add params[: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 with false 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 as video_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文