alexs333
alexs333

Reputation: 12593

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?

  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?

Upvotes: 1

Views: 123

Answers (1)

rubish
rubish

Reputation: 10907

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

Upvotes: 1

Related Questions