Ruby ternary operator in erb?
<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' %>">
Ruby ternary operator in erb with html tags
You're mixing the code and html. It would be more clear to do something like this:
<% if condition %>
"Running" <a href='/stop'> Stop Server </a>
<% else %>
"Stopped" <a href='/start'> Start Server</a>
<% end %>
syntax error ternary operator in erb
You should use parenthesis with link_to arguments. Ruby except a :
but instead he find @entry.user.nickname
. rewrite your ternary operator like that :
boolean_output ? link_to(arguments,arguments) : "something else"
Assigning values with ternary operator in ERB configuration file
It's very similar to your Java example.
envProp.serverUrl=<%= note['note_environment'] == 'production' ? '//prod.server.com' : '//other.server.com' %>
How to combine embedded ruby with a ternary operator in a view
<%= 1 == 1 ? "one is one" : "one is two" %>
# outputs "one is one"
Therefore:
<%= image_tag "/assests/#{ item.status == "1" ? "yes" : "no"}.gif" %>
However, in this case, since you are testing three possible values in all, a switch
statement inside a helper method might be best.
# app/helpers/items_help.rb
def gif_name(status)
case status
when nil
"nil"
when "1"
"yes"
else
"no"
end
end
# app/views/items/action.html.erb
<td><%= image_tag "/assests/#{gif_name(item.status)}.gif" %></td>
How do I use the conditional operator (? :) in Ruby?
It is the ternary operator, and it works like in C (the parenthesis are not required). It's an expression that works like:
if_this_is_a_true_value ? then_the_result_is_this : else_it_is_this
However, in Ruby, if
is also an expression so: if a then b else c end
=== a ? b : c
, except for precedence issues. Both are expressions.
Examples:
puts (if 1 then 2 else 3 end) # => 2
puts 1 ? 2 : 3 # => 2
x = if 1 then 2 else 3 end
puts x # => 2
Note that in the first case parenthesis are required (otherwise Ruby is confused because it thinks it is puts if 1
with some extra junk after it), but they are not required in the last case as said issue does not arise.
You can use the "long-if" form for readability on multiple lines:
question = if question.size > 20 then
question.slice(0, 20) + "..."
else
question
end
How do you write inline Ruby on Rails ternary operators in an HTML tag with white space?
You can write it like this
<li class="<%= loc == @ruby_var ? "nav-item active" : "nav-item" %>">
# ...
</li>
Note the "
outside of the erb expression.
Or you can use tag helper like this
<%= tag.li, class: ["nav-item", (:active if loc == @ruby_var)] do %>
# ...
<% end %>
I like the second option better because I prefer not to mix HTML and ERB when describing a tag.
Why is my ternary operator not working on a class in ERB to load images from a database to a bootstrap carousel?
Reason being your ternary operator are not getting index
to make it working you need to do with each_with_index
instead of each
So here is right way to do:
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<% @post.first(3).each_with_index do |image, index| %> <!--use each_with_index -->
<li data-target="#myCarousel" data-slide-to="<%= index %>" class="<%= index == 0 ? 'active' : '' %>"></li>
<% end %>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<% @post.first(3).each_with_index do |image, index| %> <!--use each_with_index -->
<div class="item <%= index == 0 ? 'active' : '' %>">
<%#= link_to image_tag(image.image.url, class:"images") %> <!--use image_tag -->
<%=image_tag image.image.url ,class: "images"%>
<div class="">
<h3><%= index %></h3>
</div>
</div>
<% end %>
</div>
<!-- Left and right controls -->
<a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
I have just fixed the issue, if images are there than it should work now. let me know for further guidance.
Ruby multiline ternary expression?
You should wrap the expressions in parenthesis:
condition ? (expression1 line 1; expression1 line 2; expression1 line 3) : expression2
You should keep in mind that this reduces readability of your code. You are probably better off using an if
/else
statement to improve readability. One resource I like to use when reviewing my ruby
code is the community style guide. As it says in the introductory paragraph:
This Ruby style guide recommends best practices so that real-world
Ruby programmers can write code that can be maintained by other
real-world Ruby programmers.
Hope this helps
Related Topics
How to Get Gmail Oauth or Xauth Tokens with Omniauth
Ruby on Rails - $ Rails Server Fails Because Uglifier Gem Could Not Be Found
Ssl_Connect Returned=1 Errno=0 State=Sslv3 Read Server Certificate B: Certificate Verify Failed MAC
Minitest, Test::Unit, and Rails
How to Replace the Characters in a String
Ruby Convert Single Quotes to Double Quotes in Xml
Ruby 'Range.Last' Does Not Give the Last Value. Why
Ruby Spreadsheet Row Background Color
Accessing a Has_One Associations' Attributes
What Orm to Use in One Process Multiple Db Connections Sinatra Application
Why Is My Ruby Git Script Hook Run with the Wrong $Path
Nokogiri Issues with Ruby on Rails
Ruby Is Already Using the Class Name of My Model
How to Access a Symbol Hash Key Using a Variable in Ruby
How to Define a Simple Global Variable in an Rspec Test That Can Be Accesed by Helper Functions