views:

62

answers:

1

Hi,

I'm trying to build a method in order to add a specific css class to my nav menu on current pages.

Here is my start: (in application_helper)

def menu_links(param)
 if request.fullpath == "#{param}_path"
   return "#{param}_path"
 end
end

in my app view:

<%= menu_links("help") %>

However, if I change request.fullpath == "#{param}_path" by request.fullpath == help_path, it works. don't know why.

Thanks for any help!

A: 

Because in the second case (request.fullpath == help_path), you use the help_path method, which expands to some URL (say http://example.com/help). In the first case, you just use a "help_path" string, which doesn't mean anything special to Rails. You probably need to use eval:

def menu_links(param)
  if request.fullpath == eval("#{param}_path")
    return eval("#{param}_path")
  end
end

Although it's not the best solution...

szeryf
Thanks szeryf for your explanation, it's much more clearer!
benoitr