【问题标题】:Problem with request.request_uri when forwarding from AJAX partial - Rails从 AJAX 部分转发时 request.request_uri 出现问题 - Rails
【发布时间】:2009-10-05 05:58:14
【问题描述】:
在我使用的 Rails 登录功能中
session[:return_to] = request.request_uri
然后在我使用的日志记录功能中:
redirect_to session[:return_to]
除非我使用 AJAX 渲染部分内容,否则效果很好。发生的情况是 request.uri 用于 AJAX 请求,它搞砸了并且没有呈现预期的内容。
你知道我该如何解决这个问题吗?
谢谢,
谭
【问题讨论】:
标签:
ruby-on-rails
ajax
partial
【解决方案1】:
这可能会做你想要的:
redirect_to :back
【解决方案2】:
request.request_uri 确实适用于 AJAX URI。我发现的唯一解决方案是让您的 AJAX 请求(或至少可能导致重定向到登录页面的请求)包含整个页面的 URI,例如
form_remote_for ... do |f|
hidden_field_tag :this_page, request.request_uri
# only works if this view itself is not loaded over AJAX!
或者更通用的方法:
form_remote_for :html => {:onsubmit => '$("this_page").value = window.location'}, ... do |f|
hidden_field_tag :this_page, request.request_uri
# non-JS clients just end up with the value given here
将其中任何一个与
结合起来
# in the login action
session[:return_to] = params[:this_page]