Forum: Ruby on Rails Easy way to convert URL back to route name?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Dave C. (Guest)
on 2009-04-04 08:27
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!!
Dave C. (Guest)
on 2009-04-04 08:46
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
bill walton (Guest)
on 2009-04-04 21:58
(Received via mailing list)
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
Dave C. (Guest)
on 2009-04-05 07:14
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
bill walton (Guest)
on 2009-04-05 08:16
(Received via mailing list)
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
pharrington (Guest)
on 2009-04-05 21:12
(Received via mailing list)
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>
Dave C. (Guest)
on 2009-04-07 06:18
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
Matt J. (Guest)
on 2009-04-08 07:03
(Received via mailing list)
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>
This topic is locked and can not be replied to.