iT邦幫忙

DAY 25
0

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

預先。

  • 分享至 

  • xImage
  •  

"在做之前先做的事"


現在如果先點過濾再點A-Z的排序會發現資料跑掉了
來把它修正吧
這裡利用了 before_action
所謂的 before_action 就是指
在跑以下的 action 之前,先跑裡面的東西
在這邊就是指在跑 index 之前,先跑一下 has_level has_search

app/controllers/

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]}")
        else
          @girbs = Girb.search(@@get_search)
        end
    else
      if params[:level_cd].present?
          @girbs = Girb.all.where(level_cd: @@get_level_cd)
      else
        if params[:tab].present?
           @girbs = Girb.all.where(level_cd: @@get_level_cd).order("name #{params[:tab]}")
        else
          @girbs = Girb.all
        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")
    render :index
  end

  def like_desc
    @at_like = true
    @girbs = Girb.all.order("like DESC")
    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

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

尚未有邦友留言

立即登入留言