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
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
On 21 April 2010 20:36, Thomas Allen <thomasmallen-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> active = request.request_uri == urlWould using url_for() not save you a lot of hassle generating that url? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Apr 21, 4:37 pm, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 21 April 2010 20:36, Thomas Allen <thomasmal...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 21 April 2010 22:00, Thomas Allen <thomasmallen-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Apr 21, 4:37 pm, Michael Pavling <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> On 21 April 2010 20:36, Thomas Allen <thomasmal...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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
On Apr 21, 5:07 pm, Michael Pavling
<pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> On 21 April 2010 22:00, Thomas Allen
<thomasmal...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> > On Apr 21, 4:37 pm, Michael Pavling
<pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> >> On 21 April 2010 20:36, Thomas Allen
<thomasmal...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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.
>
> --
> You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
> To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
> For more options, visit this group
athttp://groups.google.com/group/rubyonrails-talk?hl=en.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.