iT邦幫忙

DAY 14
2

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

刪除。

  • 分享至 

  • xImage
  •  

"記憶並不會抹去,只是你想不起來罷了"


這裡要來寫刪除的功能了
先去 controller 寫好相對應 action

app/controllers/girbs/girb_controller.rb

class GirbsController < ApplicationController
  def index
    @girbs = Girb.all
  end
  def like
    @girb = Girb.find(params[:id])
    @girb.like += 1;
    @girb.save
    redirect_to(girbs_path)
  end
  def on_click_bang
    @girb = Girb.find(params[:id])
    @girb.bangs.create
    redirect_to(girbs_path)
  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])
  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

到 index 寫好刪除的鍵結

app/views/girbs/index.htm.erb

<div class="container">
<h1>Index Page of Girb</h1>
<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) }}
直播中

尚未有邦友留言

立即登入留言