Reputation: 547
I built a RoR app where I want to allow users to delete multiple items which created_at field matches the date which the user sets in the date field of the delete form. This would be a delete form with 1 date field, and a submit button.
I guess at first I need a custom route in which the created_at date, which the user chose, will be present as a parameter.
Then I need a custom action in the items controller to get the created_at parameter from the route, use it to get all the items which belongs to the user (which means that the user id must be present as a paramater also), and were created at the created_at date. Then use a loop to delete them. It sounds fairly straightforward but still I would like to know if there is a "best paractice" to make this happen. Like how to properly set up the route for this in rails way?
Upvotes: 0
Views: 318
Reputation: 9692
A simple if statement in your destroy action should do the trick:
# DELETE /items/1
# DELETE /items/1.json
def destroy
if params[:date]
Item.where(created_at: params[:date]).destroy_all
else
Item.where(id: params[:id]).destroy
end
respond_to do |format|
format.html { redirect_to items_url }
format.json { head :no_content }
end
end
Upvotes: 2