Ruby on Rails path awareness
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 "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).
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]
.
精彩评论