Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-3.1ruby-on-rails-3.2

Creating a secondary delete option


I've create a table that gets populated with status_updates by the user over time. Each status can be deleted individually.

This is accomplished by calling the destroy method in the status_update_controller and having the particular status_update identified in a private method called correct_user.

So basically it looks like this:

At the top of my status_update_controller

before_filter :correct_user, only: :destroy

The destroy method

def destroy
    @status_update.destroy
    redirect_to root_url
end    

and the private correct user method

private

    def correct_user
      @status_update = current_user.status_update.find_by_id(params[:id])
      redirect_to root_url if @status_update.nil?
    end  

My question is this:

How can I select all of the status_updates for a particular user and destroy them using a button or link?

Thanks!


Solution

  • Define inside your StatusUpdatesController an action like this:

    def delete_all
      current_user.status_updates.delete_all
    end
    

    You will need to route to this action also:

    resources :status_updates do
      collection do
        delete :delete_all
      end
    end
    

    And to make a button:

    button_to "Delete the everything", delete_all_status_updates_path, :method => :delete