2

此操作无效:

redirect_to request.referer, :format => "html", :alert => exception.message

当它从 js 格式的控制器操作重定向时,重定向也是 js 格式,而不是 html 格式。

任何线索为什么?谢谢!

PS:我也试过 :format => :html

编辑完整性

重定向发生在通过此 application_controller 方法进行 cancan 自动化之后

   if request.referer
      logger.debug "Rescueing Cancan"
      redirect_to request.referer, :format => :html, :alert => exception.message
    else
      redirect_to root_path, :alert => exception.message, :format => :html
    end
  end

这是我的日志:

Started GET "/challenges/ground-floor/tasks" for 127.0.0.1 at 2012-12-06 11:17:06 +0100
Processing by ChallengesController#tasks as JS
  Parameters: {"id"=>"ground-floor"}
  Projectmilestone Load (0.6ms)  SELECT "projectmilestones".* FROM "projectmilestones" WHERE "projectmilestones"."id" = 790 LIMIT 1
Rescueing Cancan
Redirected to http://127.0.0.1:3000/challenges/ground-floor?subaction=pmdocuments&page=1
Completed 302 Found in 105ms (ActiveRecord: 1.6ms)


Started GET "/challenges/ground-floor?subaction=pmdocuments&page=1" for 127.0.0.1 at 2012-12-06 11:17:06 +0100
Processing by ChallengesController#show as JS
4

1 回答 1

2

尝试:back使用

redirect_to :back, alert: exception.message

从上的文档redirect_to

:back- 返回发出请求的页面。对于从多个地方触发的表单很有用。简写为redirect_to(request.env["HTTP_REFERER"])

于 2012-12-06T10:11:55.193 回答