开发者

Rails 2.3 - Redirection Issues

I have an application where folks search for an item. If the item is found, they are presented with a list of related items found by parameters in the URL AND a contact form. I'm having issues redirecting the visitor back to the same page (with the URL parameters) after submitting the form.

开发者_如何学C

Any ideas?


Try redirect_to(:back).


You could use redirect_to :back, but note that this depends on the Referer header being set, and you'll run into an error if it's not for some reason.

To get around this, I use a method like the following in my application (I put it into ApplicationController so it's accessible in all my controllers):

def redirect_back_or_to(options = {})
  if request.env["HTTP_REFERER"].blank?
    redirect_to options
  else
    redirect_to :back
  end
end

Which will redirect back if the Referer header is set and otherwise work like a normal redirect_to (so you can specify where to redirect to by default).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜