# Accessing the router from a helper

**URL:** <https://www.ruby-forum.com/t/accessing-the-router-from-a-helper/187338>\
**Category:** Rails\
**Created:** [April 21, 2010, 9:37pm UTC](https://www.ruby-forum.com/t/accessing-the-router-from-a-helper/187338 "2010-04-21T21:37:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomuhs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@tomuhs](https://www.ruby-forum.com/u/tomuhs)\
**Post date:** [April 21, 2010, 9:37pm UTC](https://www.ruby-forum.com/t/accessing-the-router-from-a-helper/187338/1 "2010-04-21T21:37:54Z")

</div>

I find manually accessing the router to be quite ugly below. Is there  
a more direct way to access it?

module ApplicationHelper

def menu\_item text, url = nil  
routes = ActionController::Routing::Routes

```
url = if url
        if url.is_a? String
          url
        else
          routes.generate url
        end
      else
        routes.generate :controller => text
      end

unless text.is_a? String
  text = t text
end

active = request.request_uri == url

"<li #{'class="active"' if active}>#{ link_to text, url }</li>"

```

end

end

Thomas

---

<div class="post-metadata">

**Author:** ![tomuhs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@tomuhs](https://www.ruby-forum.com/u/tomuhs)\
**Post date:** [April 21, 2010, 10:39pm UTC](https://www.ruby-forum.com/t/accessing-the-router-from-a-helper/187338/2 "2010-04-21T22:39:22Z")

</div>

On 21 April 2010 20:36, Thomas A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> active = request.request\_uri == url

Would using url\_for() not save you a lot of hassle generating that url?

---

<div class="post-metadata">

**Author:** ![tomuhs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@tomuhs](https://www.ruby-forum.com/u/tomuhs)\
**Post date:** [April 21, 2010, 11:01pm UTC](https://www.ruby-forum.com/t/accessing-the-router-from-a-helper/187338/3 "2010-04-21T23:01:38Z")

</div>

On Apr 21, 4:37 pm, Michael P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 21 April 2010 20:36, Thomas A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > active = request.request\_uri == url
> 
> Would using url\_for() not save you a lot of hassle generating that url?

Thanks Michael, url\_for was what I was looking for in place of  
manually grabbing the router. But why do you think that would change  
the line you quoted?

Here is the updated method:

def menu\_item text, url = {}  
unless url[:access] and active\_user.cannot? url[:access]

```
routes = ActionController::Routing::Routes

unless url.is_a? String
  if url[:controller]
    url = url_for url
  else
    url = url_for :controller => text
  end
end

unless text.is_a? String
  text = t text
end

active = request.request_uri == url

"<li#{' class="active"' if active}>#{ link_to text, url }</li>"

```

end  
end

Thomas

---

<div class="post-metadata">

**Author:** ![tomuhs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@tomuhs](https://www.ruby-forum.com/u/tomuhs)\
**Post date:** [April 21, 2010, 11:09pm UTC](https://www.ruby-forum.com/t/accessing-the-router-from-a-helper/187338/4 "2010-04-21T23:09:50Z")

</div>

On 21 April 2010 22:00, Thomas A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Apr 21, 4:37 pm, Michael P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > On 21 April 2010 20:36, Thomas A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > > active = request.request\_uri == url
> > 
> > Would using url\_for() not save you a lot of hassle generating that url?
> 
> But why do you think that would change the line you quoted?

It wouldn’t necessarily; I was just referring to its use of the “url”  
variable.

---

<div class="post-metadata">

**Author:** ![tomuhs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@tomuhs](https://www.ruby-forum.com/u/tomuhs)\
**Post date:** [April 21, 2010, 11:13pm UTC](https://www.ruby-forum.com/t/accessing-the-router-from-a-helper/187338/5 "2010-04-21T23:13:56Z")

</div>

Ah, that makes sense now. For the sake of correctness, the final code  
(with a yield and without the unused routes variable):

def menu\_item text, url = {}  
unless url[:access] and active\_user.cannot? url[:access]

```
unless url.is_a? String
  if url[:controller]
    url = url_for url
  else
    url = url_for :controller => text
  end
end

unless text.is_a? String
  text = t text
end

active = request.request_uri == url

content = link_to text, url
if block_given?
  content << yield
end

"<li#{' class="active"' if active}>#{content}</li>"

```

end  
end

Thomas
