jeremy.clark
jeremy.clark

Reputation: 852

Is there a rails way to get current route's scope?

If you have a scope in your routes.rb file such as:

scope "/account" do
  resources :items
end

How can you determine if the current page is in the 'account' section? Meaning the following items would be considered in the 'accounts' section?

/account/items
/account/item/1
/account/item/1/edit

I know this is possible by performing string comparisons on request variables but was curious if there is a 'rails way' of determining this information.

Thanks for any input.

Upvotes: 11

Views: 2678

Answers (1)

Tim Baas
Tim Baas

Reputation: 6185

I had the same problem just now.. What I did is add a parameter to the scope:

scope '/me', me_scope: true do
  # ...
end

This way I can look it up in the controller:

class ApplicationController < ActionController::Base
  def me_scope?
    params[:me_scope].eql? true
  end
end

Not the cleanest solution, but it works..

Upvotes: 13

Related Questions