Add redirect_back and deprecate redirect_to :back#22506
Add redirect_back and deprecate redirect_to :back#22506sgrif merged 2 commits intorails:masterfrom derekprior:dp-redirect_to_back_or_default
redirect_back and deprecate redirect_to :back#22506Conversation
|
r? @kaspth (@rails-bot has picked a reviewer for you, use r? to override) |
|
I think it is common pattern to use class ApplicationController < ActionController::Base
rescue_from ActionController::RedirectBackError, with: :redirect_to_default
protected
def redirect_to_default
redirect_to root_url
end
end |
|
Hello and thanks for your contribution!
You are making the point that Could you provide a real case (with or without code) in which you would still need to redirect to the previous page if the referrer is present while, at the same time, redirect to a default page if not present? That would be helpful to better understand the value of the PR. Thanks! 🎀 |
|
Thanks for the great questions @claudiob.
I largely concur, but as a consultant exposed to dozens of applications, I continue to see Consider, for example, an application that manages products. Products can be viewed in many different contexts and from each of these views you are able to "Activate" of "Deactivate" the product. The controller for activations may look like this: class ProductActivationsController < ActionController::Base
def create
Product.find(params[:product_id]).activate
redirect_to :back, notice: "Product activated"
end
def destroy
Product.find(params[:product_id]).deactivate
redirect_to :back, notice: "Product deactivated"
end
endBecause these controller actions can be called from various locations -- say from the admin products index, the admin product show, the store ui if you are an admin, or any number of various other places, the notion of "from wherever you came" is valuable. You can attempt to pass the return path explicitly as a parameter of the If I were designing this solution in a clean room, I'd deprecate Either way, I continue to see application that have dozens of not so straight-forward to solve
This can get out of hand if you have different defaults per action. It also relies on exceptions for flow control, which I'd prefer not to do. |
|
All that said, I'd totally understand not accepting this PR, but then I'd suggest deprecating |
|
I definitely agree that this should replace That said, I'm not huge on the method name, it feels a bit long and unweildly to me. |
|
I agree. If deprecating |
|
Agree on deprecation of |
|
I like |
|
What about |
|
|
|
I was about to avoid |
|
It's not always a URL. You could pass anything that worked with def create
product = Product.find(params[:product_id]
product.activate
redirect_back(fallback: product, notice: "Activated")
endIn the context of |
|
I think that On Sun, Dec 6, 2015, 10:31 AM Derek Prior notifications@github.com wrote:
|
|
@sgrif yes it is clear only if you know how I think introducing of |
|
r? @sgrif |
|
I think |
|
@sgrif updated and rebased. |
|
No deprecation of Anyway I think the documentation of |
|
Let's just go ahead and deprecate, too |
|
@sgrif Deprecation added. |
There was a problem hiding this comment.
These should all be assert_deprecated
`redirect_to :back` is a somewhat common pattern in Rails apps, but it is not completely safe. There are a number of circumstances where HTTP referrer information is not available on the request. This happens often with bot traffic and occasionally to user traffic depending on browser security settings. When there is no referrer available on the request, `redirect_to :back` will raise `ActionController::RedirectBackError`, usually resulting in an application error. `redirect_back` takes a required `fallback_location` keyword argument that specifies the redirect when the referrer information is not available. This prevents 500 errors caused by `ActionController::RedirectBackError`.
Applications that use `redirect_to :back` can be forced to 500 by clients that do not send the HTTP `Referer` (sic) header. `redirect_back` requires the user to consider this possibility up front and avoids this trivially-caused application error.
Add `redirect_to_back_or_default`
|
thanks all 🎊 |
redirect_to_back_or_defaultredirect_back and deprecate redirect_to :back
|
Sorry im a bit sad about this. :( I like rails and i like that it optimized for happiness but this doesnt make me happy. I'm ok with raises redirect errors, they are super loggable and rescuable. And then i know where this referer less stuff comes from.(ips, agents etcetc) But now i have to update all my code & rails engines etc etc. Am i missing something or does this really just do nothing except add work? Why didn't you just made a comprise: And add a rails config that raises if fallback isn't present? But people that know what they are doing can just use whatever they want and put that |
|
Hi! |
redirect_to :backis a somewhat common pattern in Rails apps, but itis not completely safe. There are a number of circumstances where HTTP
referrer information is not available on the request. This happens often
with bot traffic and occasionally to user traffic depending on browser
security settings.
When there is no referrer available on the request,
redirect_to :backwill raise
ActionController::RedirectBackError, usually resulting inan application error.
redirect_to_back_or_defaultis a helper I have added to many railsapplications to avoid this situation and now I'm proposing it be added
to Rails proper and encouraged in place of
redirect_to :back.