Newbie moving from other language - simple inline Rails conditional

P

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.

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

Best,

Peter D Bethke
 
G

Gary Wright

Hi All,
=20
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.
=20
I'm trying it first as in inline conditional, and failing miserably:
=20
<%=3D (request.fullpath =3D=3D '/home/menu') ? render :text =3D> = 'current' %>
=20
Basically, I'm trying to say "if the fullpath =3D 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':
<%=3D (request.fullpath =3D=3D '/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
 
G

Gennady Bystritsky

Hi All,
=20
I'm moving to Ruby from another language and I'm working through the basi=
cs 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.
=20
I'm trying it first as in inline conditional, and failing miserably:
=20
<%=3D (request.fullpath =3D=3D '/home/menu') ? render :text =3D> 'current= ' %>
=20
Basically, I'm trying to say "if the fullpath =3D x, then output the stri=
ng "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:
<%=3D (request.fullpath =3D=3D '/home/menu') ? render:)text =3D> 'current')=
: "" %>

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

Gennady.
 
W

Waldemar Dick

Hi,


Am 27.01.2011 16:43, schrieb Gennady Bystritsky:
On Jan 25, 2011, at 12:55 PM, Peter D Bethke wrote: [...]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top