Wednesday, May 18, 2022

[FIXED] How to create a line space in a Rails partial view?

Issue

In my index.html.erb view, I have the following render to display each and every flight (without doing a .each:

<%= render @flights %>

However, as I have the flights partial written, all the flights are listed side-by-side. What I want is one flight listed per line. How do I create a line space after each flight?

<%= radio_button_tag :flight_id, flight.id %>
<%= flight.id %>
<%= flight.date.strftime("%B %d, %Y")  %>
<%= flight.date.strftime('%I:%M:%S %p') %>
<%= flight.from_airport.code %>
<%= flight.to_airport.code %>
<%= distance_of_time_in_words(flight.duration) %>

Solution

Why not use tables?

<table>
  <%= render @flights %>
</table>

and in your partial write

<tr>
  <td><%= radio_button_tag :flight_id, flight.id %></td>
  <td><%= flight.id %></td>
  <td><%= flight.date.strftime("%B %d, %Y")  %></td>
  <td><%= flight.date.strftime('%I:%M:%S %p') %></td>
  <td><%= flight.from_airport.code %></td>
  <td><%= flight.to_airport.code %></td>
  <td><%= distance_of_time_in_words(flight.duration) %></td>
</tr>

Arrgghhhh so much typing, and so error-prone. For educational purposes, why not use haml?

%table
  = render @flights

in your partial:

%tr
  %td= radio_button_tag :flight_id, flight.id 
  %td= flight.id 
  %td= flight.date.strftime("%B %d, %Y")  
  %td= flight.date.strftime('%I:%M:%S %p') 
  %td= flight.from_airport.code 
  %td= flight.to_airport.code 
  %td= distance_of_time_in_words(flight.duration) 


Answered By - nathanvda
Answer Checked By - Timothy Miller (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.