vbotio
vbotio

Reputation: 1674

How to make an If inline using ERB template

I am trying to make a simple if...else statement in my template. Here is the solution I have currently:

<%if @collections.size > 1 %>
 coleções
<% else %>
  coleção
<% end %>

It is ugly I guess.

So I tried the following:

<% @collections.size > 1 ? 'coleções' : 'coleção' %>

But it didn't work.

How can I make this if...else statement work?

Upvotes: 5

Views: 8201

Answers (2)

Robin Daugherty
Robin Daugherty

Reputation: 7524

I know you're asking about your syntax, but please use the text helper built in to Rails rather than writing if...else statements.

<%= pluralize(@collections, 'coleção') %>

Of course, Rails supports English inflections by default, but you can add support for other languages. And here's a gist that adds Portuguese inflections.

Upvotes: 2

Ursus
Ursus

Reputation: 30056

You missed the equal sign =. You need it if you want to render something.

<%= @collections.size > 1 ? 'coleções' : 'coleção' %>

Upvotes: 15

Related Questions