iT邦幫忙

DAY 21
0

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

友善。

  • 分享至 

  • xImage
  •  

"友善一點"

現在如果我們對頁面點選Z-A排序( http://localhost:3000/girbs?tab=desc
再點選火箭(或者點選愛心)
會發現頁面重洗回最原始排序( http://localhost:3000/girbs
非常的不友善
現在就來改良它吧
其實很簡單
去 contoller 找到愛心跟火箭
把 redirect_to(girbs_path) 取代成 redirect_to :back 就可以了

app/controllers/girbs_controller.rb

class GirbsController < ApplicationController

  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")
        @tab = "asc"
      elsif @tab == "desc"
        @girbs = Girb.all.order("name DESC")
      else
        @girbs = Girb.all
      end
    end
  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)
  end
end

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

尚未有邦友留言

立即登入留言