jeudi 25 août 2016

Rails 4: Multiple Delete with checkboxes

I am trying to add multiple delete for a user via checkboxes to a table in my app but it's not working:

routes.rb (maybe I have not used proper syntax here??)

...
    resources :homeworks do 
        resources :hw_comments
        member do
          patch :complete
        end
      end

      resources :homeworks do
        collection do
        delete 'destroy_multiple'
     end
    end

homeworks_controller.rb:

def destroy
    @homework.destroy
    redirect_to homeworks_path
end

def destroy_multiple
    Homework.where(:id => params[:homework_ids]).destroy_all
    respond_to do |format|
    format.html { redirect_to homeworks_path }
    format.json { head :no_content }
end
end

View homework_details.html.erb

<%= form_tag destroy_multiple_homeworks_path, method: :delete do %>




  <table class="tableHomework wrapper3 small-6">

          <thead>
          <tr>
            .....
            <th style="width:5%;", bgcolor="#313E4E" ></th>
            .....
          </tr>
          </thead>

          <% @homeworks.each do |homework| %>
          <tbody>
          <tr>
           ....
            <td><%= check_box_tag "homework_ids[]", homework.id %></td>
            .....
          </tr>



          </tbody>
          <% end %>
        </table>
        <%= submit_tag "Delete selected" %>
        <% end %>

I tried solutions from other questions here but they were for Rails 3. The error I get upon pressing delete is "Couldn't find Homework with 'id'=destroy_multiple". Would appreciate any guidance. Thanks.




Aucun commentaire:

Enregistrer un commentaire