iT邦幫忙

DAY 17
2

Rails. Really, I love study.系列 第 17

人氣。

"人氣,那是啥?能吃嗎?"

這裡使用另一種寫法
來做 like 的排序
先去 controller 寫好兩個相對應的 action

app/controllers/girbs_controller.rb

  def like_asc
    @at_like = false
    @girbs = Girb.all.order("like ASC")
    render :index
  end

  def like_desc
    @at_like = true
    @girbs = Girb.all.order("like DESC")
    render :index
  end

在 index 寫好相對應的連結

app/views/girbs/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 %>
    
    <% if @at_like == nil %>
      <%= link_to like_asc_girbs_path do %>
      <i class="fa fa-child"></i>
      <% end %>
    <% elsif @at_like %>
      <%= link_to like_asc_girbs_path do %>
      <i class="fa fa-male" style="color:blue"></i>
      <% end %>
    <% else %>
      <%= link_to like_desc_girbs_path do %>
      <i class="fa fa-female" style="color:red"></i>
      <% end %>
    <% end %>

    </span>
  </div>

去 routes 設定好相對應的 path

config/routes.rb

Rails.application.routes.draw do
  #get 'girbs/index'

  resources :girbs do
    member do
      post :like, :on_click_bang 
    end
    collection do
      get :like_asc
      get :like_desc
    end
  end

end

鍵入 http://localhost:3000/girbs
進入畫面後點選右手邊的人形圖案
可以看到愛心數字由小而大排序
再按一次人形圖案就會變成由大而小排序囉


上一篇
排序。
下一篇
傳情。
系列文
Rails. Really, I love study.30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言