current_page? inside controller

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 ?

Robert Pankowecki

Robert Pankowecki wrote:

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 ?

Robert Pankowecki

Hi Robert,
I think what you can check is params[:controller] and params[:action].
This would give you the current location in your app.

Thanks,
Anubhaw

resources :addresses
resources :mailing_addresses, :controller => ‘addresses’ <===== this is here confuses me, why not have another controller?
this will generate 7 restful action to a controller that has
already 7 restful action

but anyway it should be like this in you routes
:requirements => { :context_type => ‘addresses’}
and then in you controller check context_type == “addresses”

I have 3 different type of addresses fot person. I can create and
display them using same views currently (but that might change in the
future) so I created one controller which recognizes which kind of
address you want to deal with now by checking the URL. In a moment
when there is some benefit of maintaining them with separate
controllers then I can change it without changing urls.
Now going to /addresses shows all of them but going to /
mailing_addresses or /registered_addresses shows only subtypes. It's
not so complicated to create another controller.

did you solve your problem?

Yes. It works ok with code that look something like that :

request.fullpath.include?(mailing_addresses_path)

Robert Pankowecki

with this
:requirements => { :context_type => ‘addresses’}
and then in you controller check context_type == “addresses”

its checked by the router instead of the controller

I still can't understand your example. What's up with this
context_type ? Let's say that I have 3 different kind of addresses.
What should I put in routes.rb and what should I check in controller ?

r.

check out how to user requirement here

http://railscasts.com/episodes/70-custom-routes

I think I started to understand your idea. You want me to write my own
custom routes for every action but in the place of URL where usually
controller name is place put my own custom requirement ?

like

get "user/:user_id/:addres_type/"
get "user/:user_id/:addres_type/new"
get "user/:user_id/:addres_type/:id"
get "user/:user_id/:addres_type/:id/edit"

?

Robert Pankowecki

i think this king of manipulation is more clear

and you dont need to declare one per resource route it works like this too

resources :mailing_addresses, :controller => 'addresses', :requirements => { :context_type => 'addresses'}

then in the controller check for context_type like this

if params[:context_type] == "addresses"

you see is almost then same but less code

the result is similar but part of the work is done by the router, and instead of checking the request object you check this