Rails 3:redirect_to with:remote =>真正

时间:2012-10-30 20:26:24

标签: 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 %>

在我的控制器中,我使用重定向结束删除功能:

def destroy
  @bookcase.destroy
  redirect_to current_user
end

这是有效的,除了它将用户重定向到'user / show.html.erb'文件而不是'user / show.js.erb'文件。如何重定向用户,指定要使用的格式?

4 个答案:

答案 0 :(得分:14)

不知道这是否回答了这个具体问题,但有些人可能会发现以下内容:

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

答案 1 :(得分:8)

我很确定你可以在redirect_to中指定格式,就像这样

redirect_to current_user, format: 'js'

答案 2 :(得分:1)

我很确定这段代码能够正常运作。

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

您可以在action / controller_name / action name /

中使用此代码

答案 3 :(得分:0)

我尝试了接受的答案,但对我没有用(第6条铁路),这对我有用:

format.js { redirect_to current_user }

希望这对某人有帮助