Ruby on Rails 路径感知

发布于 2024-11-30 00:30:06 字数 375 浏览 1 评论 0原文

让我们考虑以下情况。 可以从 Ruby on Rails 应用程序的“管理”和“配置”部分访问 products_controller

在视图中,我需要区分我当前所在的部分(即“管理”或“配置”)。实现正确结果的最佳实践是什么?

想到了几个解决方案?

  1. 附加“referrer”选项作为参数,并用它来区分我来自哪里(我认为这会非常丑陋并且破坏休息的本质)。

  2. 在控制器中创建单独的操作对(即new/createadmin_new/ admin_create)。

在给定情况下,正确的方法是什么?

Lets consider the following situation.
There is products_controller which can be accessed from "Admin" and "Configure" sections of the Ruby on Rails application.

In the view I need to differentiate which section I am currently in (i.e. "Admin" or "Configure"). What would be there best practice of achieving the right result?

Couple of solutions come to mind?

  1. Append the "referrer" option as a parameter and use it to distinguish where I came from (i think this would be super-ugly and break the nature of rest).

  2. Create separate action pairs in the controller(i.e. new/create and admin_new/ admin_create).

What would be the right approach in the given situation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

与往事干杯 2024-12-07 00:30:07

如果只是为了记录目的,添加一个参数就足够了。

如果处理事情的逻辑取决于用户来自哪里,请选择映射到不同操作的不同路线。

如果您不想添加参数,但出于日志记录目的,您还可以创建非常规路由:

 resources :products, :except => [:new, :create] do
   collection do
     get  products/new(/:section) => "products#new"
     post products(/:section) => "products#craete"
   end
 end

现在您可以拥有 new_message_path(:section => "admin") 它将生成路径 /products/new/admin,您将在 params[:section] 中获得 :section

If it is just for logging purposes, adding a parameter should be enough.

If logic of how things are handled depends on where user came from, go for different routes mapping to different actions.

If you don't wan't to add a parameter, but it is for logging purposes, you can also create non-conventional route:

 resources :products, :except => [:new, :create] do
   collection do
     get  products/new(/:section) => "products#new"
     post products(/:section) => "products#craete"
   end
 end

Now you can have new_message_path(:section => "admin") and it will result in path /products/new/admin, you will have the :section available in params[:section].

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