Ruby on Rails 路径感知
让我们考虑以下情况。 可以从 Ruby on Rails 应用程序的“管理”和“配置”部分访问 products_controller。
在视图中,我需要区分我当前所在的部分(即“管理”或“配置”)。实现正确结果的最佳实践是什么?
想到了几个解决方案?
附加“referrer”选项作为参数,并用它来区分我来自哪里(我认为这会非常丑陋并且破坏休息的本质)。
在控制器中创建单独的操作对(即new/create和admin_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?
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).
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果只是为了记录目的,添加一个参数就足够了。
如果处理事情的逻辑取决于用户来自哪里,请选择映射到不同操作的不同路线。
如果您不想添加参数,但出于日志记录目的,您还可以创建非常规路由:
现在您可以拥有
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:
Now you can have
new_message_path(:section => "admin")
and it will result in path/products/new/admin
, you will have the:section
available inparams[:section]
.