Take the 2-minute tour ×

I am trying to figure out how to put Ruby code into a HAML file so I can determine which controller the request came through, and set some appropriate settings.

I added this code to the top of the file:

  active_is = "#{request[:controller]}/#{action_name}"

But it crashed with the error saying:

Illegal nesting: nesting within plain text is illegal.

Does that mean that my syntax is off? How do I fix this?


share|improve this question
I'm pretty leery of what you're trying to do here, however, for a variety of reasons--it seems like there will be a lot of ugly logic in what should be a beautiful template. –  Dave Newton May 8 '12 at 21:41
@DaveNewton why suspicious? In a way, you kind of need to put some logic into the HAML in order to know what divs to display and how to display them, no? –  Awesomeness May 8 '12 at 21:48
Some, sure. Based on the controller? Much less common, and it makes me wonder if something a bit more abstract would be a better option. –  Dave Newton May 8 '12 at 21:57
You should use helpers if you're rendering the same view from multiple controllers. But to answer your question add a "-" just to run ruby code and "=" to run the code and display the result –  bcd May 12 '12 at 6:14
@Awesomeness Dave is right. You really shouldn't be putting this sort of logic into your views: views should be dumb. Try to write your views more or less as if you were using Mustache; you'll get much better modularization and reusability. Depending on what you're doing, the Cells gem might also be useful. –  Marnen Laibow-Koser Sep 24 '12 at 18:06
add comment

1 Answer

up vote 4 down vote accepted

Language filters are preceded by the colon, not the reverse:


More often than not, though, it's easier to just use -:

share|improve this answer
Also, since you are already using action_name I'd use controller_name for consistency, e.g. - active_is = "#{controller_name/action_name}" –  Vadym Tyemirov 2 days ago
add comment

Your Answer


