Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby ternary operator in erb?

How can I make this code look better:

<%=raw manuscript.uploaded_to_s3? ? "<span style=\"color:green;\">" : "<span style=\"color:red;\">" %>

That is, can the HTML go outside of the ERB block making this easier to read?

like image 916
Reed G. Law Avatar asked Mar 09 '11 16:03

Reed G. Law


People also ask

Does Ruby have ternary operator?

The ternary operator is a common Ruby idiom that makes a quick if/else statement easy and keeps it all on one line. The ternary operator uses a combination of the ? and : .

How ternary operator works in Ruby?

The ternary operator is used to return a value based on the result of a binary condition. It takes in a binary condition as input, which makes it similar to an 'if-else' control flow block. It also, however, returns a value, behaving similar to a function.

What is the syntax of ternary statement in Ruby?

The conditional ternary operator assigns a value to a variable based on some condition. This operator is used in place of the if-else statement. The operator first evaluates for a true or false value and then, depending upon the result of the evaluation, executes one of the two given statements​.

What is '?' In Ruby?

i know that the '?' in ruby is something that checks the yes/no fulfillment.


1 Answers

<span style="color:<%= manuscript.uploaded_to_s3? ? 'green' : 'red' %>">

I would advocate a CSS class rather than style attribute 8P:

<span class="<%= manuscript.uploaded_to_s3? ? 'green' : 'red' %>">
like image 186
Reuben Mallaby Avatar answered Oct 03 '22 22:10

Reuben Mallaby