Using Rails and HTML to View, Sort, and Search Tables.

Using Rails and HTML to View, Sort, and Search Tables.

The idea to write this blog came out during my Mod2 Project at Flatiron School, during which my Partner Austin Buhler and I decided to create a Fantasy Football Lineup Optimizer. We built a large pool of NFL Players, and very quickly I realized we had a need to sort and filter this table by multiple attributes, as well as search the table for certain values.

The idea to write this blog came out during my Mod2 Project at Flatiron School, during which my Partner Austin Buhler and I decided to create a Fantasy Football Lineup Optimizer. We built a large pool of NFL Players, and very quickly I realized we had a need to sort and filter this table by multiple attributes, as well as search the table for certain values. Unfortunately, I had no idea how to do this. So began my dive into the world of Tables, HTML, and Rails.

Creating Tables in HTML View Pages

We’ll start by defining our table of NFL Players. Each player has a name attributes, position attribute, and an aggregate ranking.

#views/players/index.html.erb

<h1>Players List</h1>
<table>
  <tr>
    <th>Player Name </th>              <-- Each of these
    <th>Position </th>                      is a column header
    <th>Avg. Ranking </th>
  </tr>
 <% @players.each do |player| %>
  <tr>
    <td><%= player.name%></td>         <-- Each of these adds
    <td><%=player.position %></td><td>     data to the corresponding
    <td><%=player.avg_ranking %></td>      column in the same row
  </tr>
  <% end %>
 </table>

This is just a combination of HTML tags and rails methods:

  • The <table> tag defines an HTML table.
  • Each table row is defined with a <tr> tag
  • Each table header is defined with a <th> tag
  • Each table data/cell is defined with a <td> tag
  • The ‘<%=’ and ‘%>’ wrappers denote Ruby code

Sorting

Now that our Table is set up, we need to add some functionality to make it easily sortable.

The first thing we’ll do is make the table headings links, so we’ll need to add link_to before each header cell’s text. The page we want each link to go to is the same page we’re on but with different query string parameters. To do that we just need to specify a hash as the second parameter to link_to.

searching ruby-on-rails programming rails sorting

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Ruby on Rails Developer | Hire RoR Developer

#1 Ruby on Rails development company. Hire Ruby on rails developer or a team to build secure, scalable and complex web solutions with a quick turnaround time.

"Rewrite your Software," says Creator of Ruby on Rails

David Heinemeier Hansson is the creator of Ruby on Rails, co-founder & CTO of Basecamp, best-selling author, Le Mans class-winning race car driver, family man, frequent podcast guest, and inspirational conference speaker.

Explain Ruby on rails MVC

Our Ruby on Rails Training will provide you to learn about Rails and web applications development with realty. Our Ruby on Rails Course also includes live sessions.

Why Ruby on Rails(RoR) is still a good choice in 2020.

Ruby on Rails in 2020: Reasons why RoR is still a viable technology in 2020 to bring your business app ideas to life with examples.

Pros & Cons you must know before using Ruby on Rails for your startup

Click here, free ruby on rails course videos for you. It shows you best way to complete Ruby certification. OnlineITGuru guides you towards easy Web script