I'm new to Rails, so forgive me if it's something simple, but it does seem as if though what I've written is correct. I'm using a ternary operator in my view to decide whether to add a class of active or not:
<li class="<% is_current_page('') ? 'active' : '' %>">
And I've debugged and know for sure that is_current_page('')
is returning true
.
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.
The ternary operator is a common Ruby idiom that makes a quick if/else statement easy and keeps it all on one line.
Functions that end with ? in Ruby are functions that only return a boolean, that is, true, or false. When you write a function that can only return true or false, you should end the function name with a question mark.
The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.
You missed =
<li class="<%= is_current_page('') ? 'active' : '' %>">
You probably wanted to do
<li class="<%= is_current_page('') ? 'active' : '' %>">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With