

Ruby on Railsで管理画面を作っています。  
リストページで全ての行の前にCheckBoxを置いて、Checkされた行を全部削除したいんです。

下記は自分のコードです。

[code]

    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'
    
[/code]

controller

[code]

    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
    
[/code]


CC BY-SA 3.0
2016-03-25T02:55:46.243
23441
2023-09-18T14:01:20.147
2016-03-25T03:01:37.617
10710
10710
question
ruby-on-rails
Railsでリストから複数の行を削除する方法


`check_box_tag`の中に配列を置き、それを用いてコントローラ側で削除すればうまくいくと思います。  
`<%= check_box_tag "words_info[]",word.id %>`  
こんな感じです。


CC BY-SA 3.0
2017-01-25T07:30:52.710
32145
2017-01-25T07:30:52.710
19915
23441
answer