Sasha
Sasha

Reputation: 6466

Redirect action not working

I'm guessing this one's pretty easy to do, but I don't know how, so I figured I'd ask.

I have a delay job in my app that calls a roll_back method in my Order model if something hasn't happened within five minutes. That method both destroys the instance it's called on and, before doing so, does a bunch of related things to associated models. The method is working just great.

The trouble is, once the method is done running, most users will be on the show page for the Order that has just been deleted, and I was hoping to redirect users to a different page once the time ran out and that roll_back method was called.

So I originally just tried this in the rollback method, right after the "self.destroy" line.

redirect_to "/orders/new", :notice => "Blah"

Didn't work, though. And at any rate, some poking around on the internet informed me that actions like redirect should be in the controller instead.

So I instead went and put that line in the Orders Controller's destroy method, right after @order.destroy. I figured when the roll_back method called self.destroy, it'd then do the redirect -- but that didn't work either, I'm guessing because the model destroy doesn't go through the controller.

So I'm not really sure how to do this. All the delay job/rollback stuff is in my model, but I'm apparently not supposed / unable to redirect users from the model. Any idea how to do this?


Tried this again, with this in my method, in case it was a routing error (I know orders_path to exist).

def destroy
    @order = Order.find(params[:id])
    @order.destroy
    redirect_to orders_path, notice: "blah"
end

Upvotes: 0

Views: 233

Answers (1)

Brad Werth
Brad Werth

Reputation: 17647

As you've already discovered, you can't respond to no request (in other words, you need a request, if you are to respond). So, what do you do when your displayed information gets stale? StackOverflow uses WebSockets to auto update some pages, including this one. You may want to check out How do real time updates work? for more information. Another technique would be to use a javascript request to make a request to your app, to verify that the order is still valid. You could easily redirect if the result indicates that the order is no longer valid. Anyway, hope this helps - good luck!

Upvotes: 1

Related Questions