現在來做姓名的排序功能
app/controllers/girb_controller.rb
def index
@tab = params[:tab]
if params[:search]
@girbs = Girb.search(params[:search]).order("name DESC")
else
if @tab == "asc"
@girbs = Girb.all.order("name ASC")
elsif @tab == "desc"
@girbs = Girb.all.order("name DESC")
else
@girbs = Girb.all
end
end
end
在 index 頁面做好相關鍵結
app/views/index.html.erb
<div class="container">
<h1>Index Page of Girb </h1>
<div>
<span class="pull-right">
<%= link_to girbs_path(tab: "asc") do %>
<i class="fa fa-sort-alpha-asc"></i>
<% end %>
<%= link_to girbs_path(tab: "desc") do %>
<i class="fa fa-sort-alpha-desc"></i>
<% end %>
</span>
</div>