Railsでリストから複数の行を削除する方法
Ruby on Railsで管理画面を作っています。
リストページで全ての行の前にCheckBoxを置いて、Checkされた行を全部削除したいんです。
下記は自分のコードです。
table.table.table-bordered.table-striped
thead
tr
th
th #
th 会社コード
tbody
- @companies.each do |company|
tr
td.text-right = company.Id
td = company.CompanyCode
td.text-center = link_to '詳細', company, class: 'badge bg-green'
td.text-center = link_to '修正', edit_company_path(company), class: 'badge bg-light-blue'
td.text-center = link_to '削除', company, data: {:confirm => '本当に削除してもよろしいでしょうか?'}, :method => :delete, class: 'badge bg-red'
controller
before_action :set_company, only: [:show, :edit, :update, :destroy]
# GET /company
def index
@companies = Company.all
end
# GET /company/1
def show
end
# GET /company/new
def new
@company = Company.new
end
# GET /company/1/edit
def edit
end
# POST /companies
def create
@company = Company.new(company_params)
if @company.save
redirect_to @company, notice: 'company was successfully created.'
else
render :new
end
end
# PATCH/PUT /companies/1
def update
if @coupon2_type.update(coupon2_type_params)
redirect_to @coupon2_type, notice: 'Coupon2 type was successfully updated.'
else
render :edit
end
end
# DELETE /companies/1
def destroy
@company.destroy
redirect_to companies_url, notice: 'Company was successfully destroyed.'
end
private
# Use callbacks to share common setup or constraints between actions.
def set_company
@company = Company.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def company_params
params.require(:company).permit(:CompanyCode)
end