开发者

Ruby on Rails path awareness

开发者 https://www.devze.com 2023-03-28 11:19 出处:网络
Lets consider the following situation. There is products_controller which can be accessed from \"Admin\" and \"Configure\" sections of the Ruby on Rails application.

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 "re开发者_如何学JAVAferrer" 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?


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].

0

精彩评论

暂无评论...
验证码 换一张
取 消