简体   繁体   中英

Deleting multiple records based on specific conditions in rails 4

Im trying to create a button that will delete all expired records from the database, but not sure exactly as to how to achieve this. I think I got the controller part set up correctly, but im not sure what to put in the routes and the code for the button itself to delete the desired records. This is what I have in my controller:

      def delete_expired
        @expired_sales = Sale.where('offer_end <= ?', Date.today)
        @expired_sales.destroy_all
        redirect_to root_path, notice: 'Successfully Deleted Sales.'
      end

I would place an ajax button somewhere on the site with the following:

link_to "Delete All", some_route_to_delete_expired_path, remote: true, method: :delete

Add to your routes.rb:

delete 'some_controller_name/delete_expired

And remove the redirect_to line as you don't need to do anything when you call this method via ajax.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM