"記憶並不會抹去,只是你想不起來罷了"
這裡要來寫刪除的功能了
先去 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>