Forum: Ruby on Rails conditional replacements in rhtml

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
171ea139761951336b844e708d1547ab?d=identicon&s=25 James B. Byrne (Guest)
on 2006-01-25 17:27
(Received via mailing list)
I am working on the Agile programming tutorial and trying out some
ruby code. Can someone tell me why the following works:

<h1>Display Cart</h1>

<p>
  Your cart currently holds <%= @items.size %>
  <%= @items.size == 1 ? "item" : "items" %>.
</p>

while this does not?

<h1>Display Cart</h1>

<p>
  Your cart currently holds <%= @items.size %>
  <%= if @items.size == 1 then "item" else "items" %>.
</p>


--
     *** e-mail is not a secure channel ***
mailto:byrnejb.<token>@harte-lyne.ca
James B. Byrne                Harte & Lyne Limited
vox: +1 905 561 1241          9 Brockley Drive
fax: +1 905 561 0757          Hamilton, Ontario
<token> = hal                 Canada L8E 3C3
D90ef6808433e63203e15a5c2dadb0bb?d=identicon&s=25 Ben Reubenstien (Guest)
on 2006-01-25 18:33
(Received via mailing list)
Hi James,

To do this with an if statement:

<%if @items.size == 1 -%>item<% else -%>items<% end -%>

I like this better myself.

<%= @items.size == 1 ? "item" : "items" %>

~ Ben
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-01-25 19:04
Ben Reubenstien wrote:
> Hi James,
>
> To do this with an if statement:
>
> <%if @items.size == 1 -%>item<% else -%>items<% end -%>
>
> I like this better myself.
>
> <%= @items.size == 1 ? "item" : "items" %>
>
> ~ Ben

This is probably more railsy.

<p>
  Your cart currently holds <%= pluralize(@items.size,"item") %>.
</p>
97cbca14d17274370cce501bbea7980a?d=identicon&s=25 Mike Harris (Guest)
on 2006-01-25 19:13
(Received via mailing list)
you forgot the end after else "items"
This topic is locked and can not be replied to.