Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to delete multiple records simultaneously using Rails 3 and multiple check_box_tag elements
    primarykey
    data
    text
    <p><strike>I'm stuck worse than a Yugo in a ditch.</strike> </p> <p>I added the changes which ultimately made this work below (marked as bold <strong>edits</strong>). </p> <p><strong>Background</strong></p> <ul> <li><em>Widgets</em> have many <em>Gadgets</em></li> <li>The final td column in the table has <code>check_box_tags</code> to select individual Gadget records for deletion</li> </ul> <p><strong>Desired Behavior</strong></p> <ul> <li>User should be able to click "checkall" as well as individual checkboxes to select Gadgets one at a time</li> <li>Clicking the "Delete Selected" button should delete checked records without refreshing the page</li> </ul> <p><strong>EDIT:</strong> <em>Adding more specific details about what's not working</em></p> <p><strong>Observed Behavior</strong></p> <ul> <li>destroy_multiple method in gadgets_controller.rb is never being called</li> <li><p>instead, the create method appears to be called</p> <p><code>Started POST "/widgets/1/gadgets" ...</code><br> <code>Processing by GadgetsController#create as JS</code><br> <code>Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"...=", "widget"=&gt;{"gadget_ids"=&gt;["all", "140", "139"]}, "widget_id"=&gt;"1"}</code><br> <code>Widget Load (0.3ms) SELECT "widgets".* FROM "widget" WHERE "widget"."id" = $1 LIMIT 1 [["id", "1"]]</code><br> <code>Rendered gadgets/create.html.haml within layouts/application (1.1ms)</code><br> <code>Rendered layouts/_header.html.haml (0.2ms)</code><br> <code>Completed 200 OK in 36ms (Views: 19.0ms | ActiveRecord: 5.0ms)</code></p></li> </ul> <p>I'm also unclear about this: <code>{"gadget_ids"=&gt;["all", "140", "139"]}</code>. </p> <p>"all" is the css id of 1 checkbox used to "checkall". In the checkbox.js.coffee file below, I attempt to remove the "all" value from the Array using javascript. When I log the Array to console, "all" is removed successfully. Yet it continues to be sent to the gadgets controller.</p> <p><strong>EDIT</strong> <em>rails remote form populates the gadgets array for the POST request. just had to set it up correctly.</em></p> <h1>Models</h1> <p><strong>widget.rb</strong> </p> <pre><code>class Widget &lt; ActiveRecord::Base has_many :gadgets validates_presence_of :name end </code></pre> <p><strong>gadget.rb</strong> </p> <pre><code>class Gadget &lt; ActiveRecord::Base belongs_to :widget validates_presence_of :widget_id end </code></pre> <h1>Gadgets Controller</h1> <pre class="lang-rb prettyprint-override"><code>class GadgetsController &lt; ApplicationController include ApplicationHelper before_filter :get_widget before_filter :widget_gadgets, only: [ :index ] respond_to :json def json_widgets [ @widget, @gadgets ] end def index respond_with(json_widgets) do |format| format.json format.html end end def new @gadget = @widget.gadgets.new() end def create @gadget=@widget.gadgets.new(params[:gadget]) if @gadget.save flash[:success] = "Gadget was successfully created" redirect_to widget_gadgets_path end end def destroy_multiple gadget_ids=(params[:gadget_ids]) to_delete=[] gadget_ids.split(',').each do |gadget_id| to_delete.push(Gadget.find(gadget_id)) end to_delete.each do |del| del.destroy() end flash.now[:success] = "Gadget Destroyed Successfully" respond_to do |format| format.html { redirect_to widget_gadgets_path(@widget) } format.json { render :json =&gt; to_delete.to_json } end end def destroy @gadget = gadget.find(params[:gadget_id]) @widget.gadgets.destroy(@gadget.id) respond_to do |format| format.html { redirect_to(widget_gadgets_path) } end end def get_widget begin @widget = Widget.find(params[:widget_id]) rescue ActiveRecord::RecordNotFound render file: "public/404.html", status: 404 end end private def widget_gadgets @widget=Widget.find(params[:widget_id]) @gadgets=@widget.gadgets unless @widget.gadgets.nil? end end </code></pre> <h1>routes.rb</h1> <p>I'm trying to use a <em>collection do</em> block. Is that the right way to implement a destroy_multiple route?</p> <pre class="lang-rb prettyprint-override"><code>DestroyMultiple::Application.routes.draw do resources :widgets resources :gadgets, :only =&gt; [ :new, :create ] resources :widgets do resources :gadgets do collection do post :destroy_multiple end end end match "/:id" =&gt; 'widget#gadgets' root to: 'widgets#index' end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload