Newbie moving from other language - simple inline Rails conditional

Hi All,

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.

Ruby is pretty cool, I'm enjoying it immensely.

Best,

Peter D Bethke

Hi All,

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).

Gary Wright

···

On Jan 25, 2011, at 3:55 PM, Peter D Bethke wrote:

Hi All,

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.

The correct syntax for the construct you use is:

cond ? expr_true : expr_false

in your case there's no expr_false. So re-wright it to something like:
<%= (request.fullpath == '/home/menu') ? render(:text => 'current') : "" %>

Or if you really do not need the false expression, you can do something like
<%= render(:text => 'current') if (request.fullpath == '/home/menu') %>

Gennady.

···

On Jan 25, 2011, at 12:55 PM, Peter D Bethke wrote:

Ruby is pretty cool, I'm enjoying it immensely.

Best,

Peter D Bethke

Hi,

[...]

Basically, I'm trying to say "if the fullpath = x, then output the string "current". I'm sure that the answer is simple.

The correct syntax for the construct you use is:

cond ? expr_true : expr_false

in your case there's no expr_false. So re-wright it to something like:
<%= (request.fullpath == '/home/menu') ? render(:text => 'current') : "" %>

Or if you really do not need the false expression, you can do something like
<%= render(:text => 'current') if (request.fullpath == '/home/menu') %>

Do you need to call 'render' at all, because you are using already an "expression result substitution" (<%=). So actually, you only nead to write:

<%= request.fullpath == '/home/menu' ? 'current' : '' %>

or

<% if request.fullpath == '/home/menu' then %>
current
<% end %>

Waldemar

···

Am 27.01.2011 16:43, schrieb Gennady Bystritsky:

On Jan 25, 2011, at 12:55 PM, Peter D Bethke wrote: