6

I have a delete link that makes a remote call:

<%= link_to image_tag("trash.png"), [current_user, bookcase], method:  :delete, :remote => true, confirm: "You sure?", title:   bookcase.image %>

In my controller, I end the delete function with a redirect:

def destroy
  @bookcase.destroy
  redirect_to current_user
end

This works, except it's redirecting the user to the 'user/show.html.erb' file instead of the 'user/show.js.erb' file. How can I redirect the user, specifying which format to use?

nullnullnull
  • 8,039
  • 12
  • 55
  • 107
  • 1
    It should be render instead of redirect_to, ajax requests cannot redirect. I think the request that is sent to the server is not a ajax request, but a delete(post) request – Manjunath Manoharan Oct 30 '12 at 21:00
  • Is it possible to remotely send a delete[post] request? – nullnullnull Oct 30 '12 at 21:02
  • Can you check the server log and tell me whether the incoming request was a xhr or a post after clicking on the delete link – Manjunath Manoharan Oct 30 '12 at 21:05
  • 1
    It's a DELETE request and it's getting processed via JS. I've found another solution that works, though. Instead of redirecting, I've created a destroy.js.erb file that renders the same partial rendered by 'users/show.html.erb'. This works perfectly, though perhaps there's a more elegant solution? – nullnullnull Oct 30 '12 at 21:08
  • then the answer below is wrong. clarify and post your own answer – Manjunath Manoharan Oct 30 '12 at 21:09
  • I believe I see what you're saying, but the answer below still works. That is, it's delivering the expected results without throwing any errors. – nullnullnull Oct 30 '12 at 21:39

4 Answers4

16

Don't know if this is answering this specific question, but some might find the following helpful:

module AjaxHelper
  def ajax_redirect_to(redirect_uri)
    { js: "window.location.replace('#{redirect_uri}');" }
  end
end

class SomeController < ApplicationController
  include AjaxHelper

  def some_action
    respond_to do |format|
      format.js { render ajax_redirect_to(some_path) }
    end
  end
end
Louis Sayers
  • 2,162
  • 3
  • 30
  • 53
8

I'm pretty sure you can specify the format in the redirect_to like this

redirect_to current_user, format: 'js'

Leo Correa
  • 19,131
  • 2
  • 53
  • 71
  • Your answer is right, and furthermore it looks like rails will default to using js when using :remote => true. I was experiencing this problem because I had actually taken a slightly different approach to AJAX. Although it has some drawbacks, I've gone ahead and changed my code to use the more conventional :remote => true in this instance. Thanks! – nullnullnull Oct 30 '12 at 20:44
  • You can always add dataType to your ajax so you can have full control of your ajax instead of using Rails remote: true – Leo Correa Oct 30 '12 at 20:45
  • It should be render instead of redirect_to, ajax requests cannot redirect. I think the request that is sent to the server is not a ajax request, but a delete(post) request – Manjunath Manoharan Oct 30 '12 at 20:59
  • http://stackoverflow.com/questions/282429/returning-redirect-as-response-to-xhr-request – Leo Correa Oct 30 '12 at 21:10
2

I am pretty sure this code will work.

render :js => "window.location = '/jobs/index'

You can use this code in action /controller_name/action name/

Sobin Sunny
  • 1,121
  • 10
  • 14
0

I tried the accepted answer but didn't work for me (RAILS 6), what worked for me is this :

format.js { redirect_to current_user }

Hope this help someone

medBouzid
  • 7,484
  • 10
  • 56
  • 86