If Else Statements in .Html.Erb in Views

If else statements in .html.erb in views

Unless you can think of a way to re-write this as a helper method, you're basically stuck with it looking kind of ugly. That's just how ERB is, as it was intended to be a minimal way of injecting Ruby into an otherwise plain-text template, not as something necessarily streamlined or elegant.

The good news is a syntax-highlighting editor will usually make your <% ... %> ERB blocks look visually different from your HTML so that can dramatically improve readability.

It's also why other representations like HAML have been created where that syntax is a lot less cluttered:

- if some_condition_previusly_established_in_a_controller
.one 123
- else
.two something else

if, elsif, else statements html erb beginner

The problem is that your first condition is true, so it stops there. Your first condition:

<% if logged_in? %>

Even if they don't have a booth it will never reach the elsif because the first condition is true. You either need:

<% if logged_in? && has_booth?(current_user.id) %>
// code
<% elsif logged_in? && !has_booth?(current_user.id) %>
// code
<% else %>
// code
<% end %>

Or it might be a cleaner approach to separate them into two if/else:

<% if logged_in? %>
<% if has_booth?(current_user.id) %>
// code
<% else %>
// code
<% end %>
<% else %>
// code
<% end %>

If condition in html.erb rails

If you have access to @time and status is an attribute of it, with a value that can be "pending" (thing you didn't add in the question), then you can do:

<% if @time.status == 'pending' %>
<!-- place edit and delete -->
<% else %>
<% @value.each do |s| %>
<p>
<strong>Message:</strong>
<%= s.message %>
</p>

<p>
<strong>Date</strong>
<%= s.date %>
</p>

<p>
<strong>Status:</strong>
<%= s.status %>
</p>

<p>
<%= link_to 'Edit', value(), %>
<%= link_to 'Delete', value(), %>
</p>
<% end %>
<% end %>

How to write if statement in ruby erb html table

The else needs to be in an ERB tag too.

Furthermore, you need to switch the usage of <%= and <%. Because you want to output the image tags, use <%=with the image_tag. But you do not output the result of the if condition, use <% with the if, else and end.

<td>
<% if a.image.exists? %>
<%= image_tag(a.image, width:100) %>
<% else %>
<%= image_tag("No_image.jpg", width:100) %>
<% end %>
<td>

To simplify the view I would consider adding a helper method to your a model (I guess it is an Auction) and just call that helper method in the view instead of having the condition in the view:

# in the model
FALLBACK_IMAGE_PATH = 'No_image.jpg'

def image_path_with_fallback
a.image.exists? ? a.image : FALLBACK_IMAGE_PATH
end

# in the view
<td><%= image_tag(a.image_path_with_fallback, width:100) %><td>

Syntax for conditions(if/else) when views should be set as a condition in RoR?

Those conditions are syntatically correct, you just need to get your logic straight. If those index.html.erb and about.html.erb are to check current file being rendered you should instantiate their names in you controller in some variable to compare, or use the :controller and :action params.

Maybe you need something like:

<% if params[:action] == 'index' %>
<%= image_tag("index_background.jpg", alt: "index background") %>
<% elsif params[:action] == 'about' %>
<%= image_tag("about_background.jpg", alt: "index background") %>
<% else %>
<%= image_tag("default_background.jpg", alt: "index background") %>
<% end %>

Why does this if statement in my application.html.erb not work like I expect?

This is because you don't check for the action_name in your first condition, but instead set it.

action_name = "index"

See also the server logs:

warning: found = in conditional, should be ==



Related Topics



Leave a reply



Submit