On Jan 25, 2011, at 3:55 PM, Peter D Bethke wrote:
I’m moving to Ruby from another language and I’m working through the basics by
altering an existing Rails project. One of the basic things I’d like to do is to
highlight a link for example with the css id “current” when the current url
matches a certain string.
I’m trying it first as in inline conditional, and failing miserably:
<%= (request.fullpath == ‘/home/menu’) ? render :text => ‘current’ %>
Basically, I’m trying to say "if the fullpath = x, then output the string
“current”. I’m sure that the answer is simple.
This is closer to a Rails/framework question than a Ruby language
question. Although it can be difficult for someone new to the language
and framework to know where one ends and the other begins. Rails
questions should generally go to the Rails forum though. In any case…
If you are already working with a view, you don’t need to use ‘render’:
<%= (request.fullpath == ‘/home/menu’) ? ‘current’ : ‘’ %>
That will evaluate to the string ‘current’ or the empty string depending
on full path. There are lots of other variations of course that will
work. You only need to utilize ‘render’ in a controller to specify how
to generate the view and even then generally only if you want to render
something other than what is implied via the conventions built into
Rails (i.e. a new request automatically renders the ‘new’ view, an edit
request automatically renders the ‘edit’ view).