iT邦幫忙

DAY 26
0

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

分頁。

  • 分享至 

  • xImage
  •  

"資料太多,幾個一數是好法"

來做分頁功能

Gemfile

source 'https://rubygems.org'

gem 'faker'
gem 'font-awesome-sass', '~> 4.2.0'
gem 'bootstrap-sass'
gem 'annotate'
gem 'simple_enum', '~> 2.0.0' , require: 'simple_enum/mongoid'
gem 'will_paginate'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'





app/controllers/girbs_controller.rb

class GirbsController < ApplicationController

  @@get_level_cd = -1
  @@get_search = ""
  before_action :has_level, :has_search

  def index
    if @@get_search != ""
        if params[:tab].present?
          @girbs = Girb.search(@@get_search).order("name #{params[:tab]}").paginate(page: params[:page], per_page: 12)
        else
          @girbs = Girb.search(@@get_search).paginate(page: params[:page], per_page: 12)
        end
    else
      if params[:level_cd].present?
          @girbs = Girb.all.where(level_cd: @@get_level_cd).paginate(page: params[:page], per_page: 12)
      else
        if params[:tab].present?
           @girbs = Girb.all.where(level_cd: @@get_level_cd).order("name #{params[:tab]}").paginate(page: params[:page], per_page: 12)
        else
          @girbs = Girb.all.paginate(page: params[:page], per_page: 12)
        end
      end
    end
  end

  # def index
  #   @tab = if %w(asc desc).include?(params[:tab]) then params[:tab] else :asc end
  #   @girbs = Girb.level_list(params[:level_cd]).search(params[:search]).order(name: @tab)
  # end


  def like_asc
    @at_like = false
    @girbs = Girb.all.order("like ASC").paginate(page: params[:page], per_page: 12)
    render :index
  end

  def like_desc
    @at_like = true
    @girbs = Girb.all.order("like DESC").paginate(page: params[:page], per_page: 12)
    render :index
  end

  def like
    @girb = Girb.find(params[:id])
    @girb.like += 1;
    @girb.save
    # redirect_to(girbs_path)
    redirect_to :back
  end

  def on_click_bang
    @girb = Girb.find(params[:id])
    @girb.bangs.create
    # redirect_to(girbs_path)
    redirect_to :back
  end

  def new
    @girb = Girb.new
  end

  def create
    @girb = Girb.new(girb_params)
    if @girb.save
      redirect_to(girbs_path)
    else
      render :new
    end
  end

  def show
    @girb = Girb.find(params[:id])
    # @comment = @girb.comments.build
  end

  def edit
    @girb = Girb.find(params[:id])
  end

  def update
    @girb = Girb.find(params[:id])
    if @girb.update(girb_params)
      redirect_to girbs_path
    else
      render :edit
    end
  end

  def destroy
    @girb = Girb.find(params[:id])
    @girb.destroy
    redirect_to girbs_path
  end

  private

  def girb_params
    params.require(:girb).permit(:name, :email, :like, :level_cd)
  end

  def has_level
    if params[:level_cd].present?
      if(nil != params[:level_cd].present?)
        @@get_level_cd = params[:level_cd]
        @@get_search = ""
      end
    end
    @@get_level_cd
  end

  def has_search
    if params[:search].present?
      if(nil != params[:search].present?)
        @@get_search = params[:search]
        @@get_level_cd = -1
      end
    end
    @@get_search
  end

end





app/views/girbs/index.html.erb

<div class="container">
  <h1>Index Page of Girb ( <%= @girbs.count %> ) 
    <% if @girbs.count != Girb.all.count %>( Filter: <%= @girbs.first.level %> ) <% end %></h1>
    <%= will_paginate @products %>
  <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>
  <%= form_tag(girbs_path, method: :get, id: "search-form") do %>
  <%= text_field_tag :search, params[:search], placeholder: "Search Name" %>
  <%= submit_tag "Search", name: nil %>
  <% end %>
  <p></p>
  <div class="row">
    <% @girbs.each do |girb| %>
    <div class="col-lg-4 col-md-6">
      <div class="panel panel-default">
        <div class="panel-body">
          <%= link_to girb_path(girb) do %>
          <div style="font-size:22px;color:#0099FF">  
            <%= girb.name %>  
            <span>
            <%= link_to edit_girb_path(girb) do %>
            <i class="fa fa-pencil-square-o" style="color:green"></i>
            <% end %>
            </span>
            <span>
            <%= link_to girb_path(girb), method: :delete, data: {confirm: "Are you sure you want to delete?"} do %>
            <i class="fa fa-trash" style="color:purple"></i>
            <% end %>
            </span>
          </div>
          <% end %>
          <div ><%= girb.email %></div>
          <div style="padding-top:3px">
            <%= link_to like_girb_path(girb.id), method: :post do %>
            <i class="fa fa-heart" style="color:red"> <%= girb.like %></i>
            <% end %>
            <span class="pull-right">
            <%= link_to on_click_bang_girb_path(girb.id), method: :post do %>
            <i class="fa fa-rocket" > <%= girb.bangs.size %></i>
            <% end %>
            </span>
          </div>
        </div>
      </div>
    </div>
    <% end %>
  </div>

</div>

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

尚未有邦友留言

立即登入留言