Search code examples
javascriptruby-on-railsdeviseaxiosturbolinks

turbolinks / redirects after a delete


Having a delete response handled via js, I'm reloading the curent page via turbolinks. In other words :

// onclick setup encapsulates the following
axios({
  method: "delete",
  url: e.target.getAttribute("href"),
  headers: Csrf() // passes the csrf token to keep things 'rails'
}).then(() => {
  Turbolinks.visit(locationWithoutHash, { action: "replace" });
});

Our signout link is handled like so.

Signing out then redirects to the root_url.

The problem is, Turbolinks.visit seems to preserve the initial method :

Started DELETE "/auth/sign_out" for 127.0.0.1 at 2018-04-22 11:21:12 +0200
Processing by Devise::SessionsController#destroy as HTML
...stuff
Redirected to http://demodemo.lvh.me:3000/
Completed 302 Found in 11ms (ActiveRecord: 1.9ms)


Started DELETE "/" for 127.0.0.1 at 2018-04-22 11:21:12 +0200
ActionController::RoutingError (No route matches [DELETE] "/"): etc...

It might just be a matter of syntax/grammar which I could not spot within the source code

Without discussing the fact that such behaviour makes sense or not, is it possible to specify a method to Turbolinks.visit in order to make sure it effectively passes a GET ? (as I'm just using it to reload the current page no matter what happens, it will always be a GET)


Solution

  • It's happening when the destroy method is like this after delete redirect_to request.referer or any URL something like this if you has like this then change to like this

    def destroy
        @obj = Model.find(params[:id])
    
        respond_to do |format|
            if @obj.destroy
                flash[:error] = 'Deleted'
                format.html { redirect_to request.referer, status: 303  }
                format.js { redirect_to request.referer, status: 303  }
            end
        end
    end
    

    I think it will help