Ajax Rails 3:将_重定向到with:remote=>;真的

Ajax Rails 3:将_重定向到with:remote=>;真的,ajax,ruby-on-rails-3,redirect,Ajax,Ruby On Rails 3,Redirect,我有一个进行远程呼叫的删除链接: <%= link_to image_tag("trash.png"), [current_user, bookcase], method: :delete, :remote => true, confirm: "You sure?", title: bookcase.image %> 除了将用户重定向到“user/show.html.erb”文件而不是“user/show.js.erb”文件之外,这是可行的。如何重定向用户,指定要使用的

我有一个进行远程呼叫的删除链接:

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

除了将用户重定向到“user/show.html.erb”文件而不是“user/show.js.erb”文件之外,这是可行的。如何重定向用户,指定要使用的格式?

我很确定您可以在重定向中指定格式,如下所示


重定向到当前用户,格式为:“js”

不知道这是否回答了这个特定问题,但有些人可能会发现以下内容很有帮助:

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
模块AjaxHelper
def ajax_redirect_to(redirect_uri)
{js:“window.location.replace('#{redirect_uri}');“}”
结束
结束
类SomeController
我很确定这段代码会起作用

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

您可以在action/controller\u name/action name/

中使用此代码。我尝试了接受的答案,但对我无效(RAILS 6),对我有效的是:

format.js { redirect_to current_user }

希望这对其他人有所帮助

你的答案是正确的,而且看起来rails在使用:remote=>true时默认使用js。我遇到这个问题是因为我实际上对AJAX采取了稍微不同的方法。尽管它有一些缺点,但我还是继续修改了代码,在本例中使用了更传统的:remote=>true。谢谢您可以始终将数据类型添加到ajax中,这样您就可以完全控制ajax而不是使用Rails remote:TrueT应该是render而不是redirect_to,ajax请求不能重定向。我认为发送到服务器的请求不是ajax请求,而是delete(post)请求。它应该呈现而不是重定向到,ajax请求不能重定向。我认为发送到服务器的请求不是ajax请求,而是delete(post)请求可以远程发送delete[post]请求吗?您能检查服务器日志并在单击delete链接后告诉我传入的请求是xhr还是post吗?这是一个delete请求,正在通过JS进行处理。不过,我找到了另一个有效的解决方案。我没有重定向,而是创建了一个destroy.js.erb文件,该文件呈现由“users/show.html.erb”呈现的相同部分。这是完美的,虽然可能有一个更优雅的解决方案?那么下面的答案是错误的。澄清并张贴你自己的答案
format.js { redirect_to current_user }