nullnullnull
nullnullnull

Reputation: 8189

Rails 3: redirect_to with :remote => true

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?

Upvotes: 6

Views: 16120

Answers (4)

medBouzid
medBouzid

Reputation: 8422

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

Upvotes: 0

Sobin Sunny
Sobin Sunny

Reputation: 1171

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/

Upvotes: 2

Louis Sayers
Louis Sayers

Reputation: 2219

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

Upvotes: 16

Leo Correa
Leo Correa

Reputation: 19829

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

redirect_to current_user, format: 'js'

Upvotes: 9

Related Questions