Joel Dehlin
Joel Dehlin

Reputation: 1019

How do I use Ruby in an HTML document

I have the following HTML in my index file.

<% @posts.each_with_index do |post, index| %>
  <tr>
    <td><%= index+1 %></td>
    <td><%= post.team_elo %></td>
    <td><%= post.team_name %></td>
  </tr>
<% end %>

I have a function called is_eligible which takes post.team_name as an argument. I want to run it each "do iteration" and skip all three printing steps if it returns false

I'm not sure how to

  1. include the file is_eligible is in and
  2. structure the code within the html somewhere.

Any ideas?

Upvotes: 0

Views: 99

Answers (2)

Thomas Ruiz
Thomas Ruiz

Reputation: 3661

# The view
<% @posts.each_with_index do |post, index| %>
  <% unless post.is_eligible? %>
    <tr>
      <td><%= index+1 %></td>
      <td><%= post.team_elo %></td>
      <td><%= post.team_name %></td>
    </tr>
  <% end %>
<% end %>

# The model post.rb
def is_eligible?
  # and there you use self.name to access the name
end

Upvotes: 4

xdazz
xdazz

Reputation: 160963

<% @posts.select{|p| is_eligible(p. team_name)}.each_with_index do |post, index| %>
  <tr>
    <td><%= index+1 %></td>
    <td><%= post.team_elo %></td>
    <td><%= post.team_name %></td>
  </tr>
<% end %>

Upvotes: 1

Related Questions