Search code examples
ruby-on-railsruby-on-rails-3web-applicationsruby-on-rails-3.1haml

how to make a clickable link to sort a table on webpage , ror


I am new to the ROR, and I am trying out some basic syntax

If I have a table of records display all my record at index.html.haml. And I want to make it sortable by some attributes of the table.

For examples

name age

bing 32
gee 20

I want to make the name and the age is clickable, so that i can sort the table by that key. The problem is I am confused about the logics.

What I got so far is I know that I have to modified the index.html.haml , and then send back some data to the controller, and then the controller to do some sort of ruby sorting, and then send back the table to the view.

can somebody provide some guidances how the implmenetation level work.


Solution

  • You can learn Ror basics from a huge number of Ror books, but just to make you started

    In the controller:

    @records = YourTable.all.sort_by { |r| r.bing }
    

    In the view:

    <% @records.each do |record| %>
      <%= record %>
    <% end %>