Easy way to convert URL back to route name?

Hi all,

I’m looking for a way to do the reverse of what the named route helpers
do. Namely:

Given routes:

map.foo ‘/foo/:id’, :controller => ‘foo’, :action => ‘show’
map.bar ‘/bar/:id’, :controller => ‘bar’, :action => ‘show’

Given urls:

/foo/1
/bar/1

I’d like to be able to do something like this in my layout:

if requested_foo_path

… show something specific to foo

elsif requested_bar_path

… show something specific to bar

end

There’s probably a better way to do this, but I’m new to rails so any
help appreciated.

Thanks!!

Playing around I came up with this solution for now:

map.foo ‘/foo/:id’, :controller => ‘foo’, :action => ‘show’,
:showing_foo => true
map.bar ‘/bar/:id’, :controller => ‘bar’, :action => ‘show’,
:showing_bar => true

if params[:showing_foo]

… show something specific to foo

elsif params[:showing_bar]

… show something specific to bar

else

… show something generic

end

I’d still like to know if there is a better way though. If anyone has
any info I’d appreciate learning the “Rails Way” to solve this problem,
if that is not it.

Thanks!!
-dave

Hi Dave,

On Sat, 2009-04-04 at 06:27 +0200, Dave C. wrote:

There’s probably a better way to do this, but I’m new
to rails so any help appreciated.

I’m not sure if this is what you’re looking for, but views get
‘knowledge’ of their ‘source’ via controller.controller name and
controller.action_name.

So you can do:

if controller.controller_name == ‘foo’

… show something specific to foo

elsif controller.controller_name == ‘bar’

… show something specific to bar

end

If you want to make the code specific to both controller and action you
can do:

if controller.controller_name == ‘foo’ && controller.action_name ==
‘show’

HTH,
Bill

Hi Dave,

On Sun, 2009-04-05 at 05:14 +0200, Dave C. wrote:

bill walton wrote:

I’m not sure if this is what you’re looking for, but views get
‘knowledge’ of their ‘source’ via controller.controller name and
controller.action_name.

Thanks Bill! I actually knew about that and wrote a similar capability
for some ASP work I do at work in my day job. Argh. Can’t believe I
didn’t think of that this time. Goes to show what happens when most of
your hobby coding is between 10pm and 1am…

:wink: Yep!

Thanks again, worked perfectly!

You’re welcome. Glad it helped.

Best regards,
Bill

bill walton wrote:

I’m not sure if this is what you’re looking for, but views get
‘knowledge’ of their ‘source’ via controller.controller name and
controller.action_name.

Thanks Bill! I actually knew about that and wrote a similar capability
for some ASP work I do at work in my day job. Argh. Can’t believe I
didn’t think of that this time. Goes to show what happens when most of
your hobby coding is between 10pm and 1am…

Thanks again, worked perfectly!

-dave

Alternatively, you can just use the current_page? method:

if current_page? foo_url
#do some ish
elsif current_page? bar_url

other stuff…

On Apr 4, 12:27 am, Dave C. [email protected]

One final thing - you can also pass extra params in :requirements -
thus:

map.resource :foos, :requirements => { :extra => ‘foo’ }

will add :extra => ‘foo’ to the incoming params. I’m not 100% sure if
this works on map.connect style routes, but if it doesn’t, it’s
probably a bug.

–Matt J.

On Apr 6, 10:18 pm, Dave C. [email protected]

pharrington wrote:

Alternatively, you can just use the current_page? method:

if current_page? foo_url
#do some ish
elsif current_page? bar_url

other stuff…

Hey, I like that too – thanks!

-dave