1

Is there any easy way of using something like current_page?method from ActionView::Helpers::UrlHelper inside controller?

I have routing like:

resources :addresses
resources :mailing_addresses, :controller => 'addresses'

And I would like to do a check in my controller that would look like this:

class AddressesController

  def index
    if current_page?(live_addresses_path)
       # ... logic goes here
    elsif current_page?(addresses_path)
       # ... logic goes here
    end
  end

end

What's the easiest way of achieving it?

Uzbekjon
  • 11,655
  • 3
  • 37
  • 54
Sasha
  • 20,424
  • 9
  • 40
  • 57
  • http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-original_url this way you can do `request.original_url` or `request.original_fullpath` – Rajarshi Das Jun 05 '16 at 13:41

1 Answers1

2

Unlike many other helper methods, UrlHelper is not included in your controllers. So, if you want to use it in your controller, you can simply include it there and use it:

class AddressesController

  include ActionView::Helpers::UrlHelper

  def index
    if current_page?(live_addresses_path)
       # ... logic goes here
    elsif current_page?(addresses_path)
       # ... logic goes here
    end
  end

end
Uzbekjon
  • 11,655
  • 3
  • 37
  • 54
  • 1
    Thank you for your answer but having another error now: `arguments passed to url_for can't be handled. Please require routes or provide your own implementation`. I tried to add routes like `include Rails.application.routes.url_helpers` but still nothing :( – Sasha Jun 05 '16 at 15:08
  • 1
    Are you passing strings to `current_page?` as in your example code or some other arguments? – Uzbekjon Jun 05 '16 at 15:37