# Easy way to convert URL back to route name?

**URL:** <https://www.ruby-forum.com/t/easy-way-to-convert-url-back-to-route-name/164696>\
**Category:** Rails\
**Created:** [April 4, 2009, 6:27am UTC](https://www.ruby-forum.com/t/easy-way-to-convert-url-back-to-route-name/164696 "2009-04-04T06:27:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [April 4, 2009, 6:27am UTC](https://www.ruby-forum.com/t/easy-way-to-convert-url-back-to-route-name/164696/1 "2009-04-04T06:27:05Z")

</div>

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!!

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [April 4, 2009, 6:46am UTC](https://www.ruby-forum.com/t/easy-way-to-convert-url-back-to-route-name/164696/2 "2009-04-04T06:46:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [April 4, 2009, 7:58pm UTC](https://www.ruby-forum.com/t/easy-way-to-convert-url-back-to-route-name/164696/3 "2009-04-04T19:58:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [April 5, 2009, 6:16am UTC](https://www.ruby-forum.com/t/easy-way-to-convert-url-back-to-route-name/164696/4 "2009-04-05T06:16:21Z")

</div>

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…

😉 Yep!

> Thanks again, worked perfectly!

You’re welcome. Glad it helped.

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [April 5, 2009, 5:14am UTC](https://www.ruby-forum.com/t/easy-way-to-convert-url-back-to-route-name/164696/5 "2009-04-05T05:14:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [April 5, 2009, 7:12pm UTC](https://www.ruby-forum.com/t/easy-way-to-convert-url-back-to-route-name/164696/6 "2009-04-05T19:12:39Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [April 8, 2009, 5:03am UTC](https://www.ruby-forum.com/t/easy-way-to-convert-url-back-to-route-name/164696/7 "2009-04-08T05:03:54Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [April 7, 2009, 4:18am UTC](https://www.ruby-forum.com/t/easy-way-to-convert-url-back-to-route-name/164696/8 "2009-04-07T04:18:54Z")

</div>

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
