Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add redirect_back functionality #1168

Merged
merged 3 commits into from
Jun 8, 2020

Conversation

matthewmcgarvey
Copy link
Member

Purpose

Fixes #1158

redirect_back allows an action to send the user back to where they made the request from. This is really useful in situations like submitting a lead form where the app might have it in several locations but the app doesn't need to take them anywhere in particular after they submit it. Rather than sending the user to a specific place after submitting the form, we can now send them back to where they originally submitted it.

This was largely taken from Rails. source

Sidenote

If you look at the Rails code, there is functionality around limiting Referer redirects in some situations using the allow_other_hosts named argument. If it is set to false then it will not redirect to a Referer header that has a different host than the request host. So, if the app is running at example.com, it will not redirect to a Referer header of differenturl.com when set to false. While this might be useful, I didn't want to copy it over just to copy it.

Description

Usage looks like:

class NewsletterSignupsCreate < BrowserAction
  post "/newsletter/signup" do
    # do the signup code
    redirect_back fallback: Home::Index
  end
end

It's important that the fallback is required because the Referer header is not guaranteed so we need to send the user somewhere.

Checklist

  • - An issue already exists detailing the issue/or feature request that this PR fixes
  • - All specs are formatted with crystal tool format spec src
  • - Inline documentation has been added and/or updated
  • - Lucky builds on docker with ./script/setup
  • - All builds and specs pass on docker with ./script/test

Copy link
Member

@jwoertink jwoertink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does look great. The only thing I'm wondering is redirect allows for a HTTP::Status to be passed... I'm wondering if we should also include a method overload for that....

@matthewmcgarvey
Copy link
Member Author

@jwoertink Good catch, there are just sooo many overloads!

@jwoertink
Copy link
Member

😂 yup, but this is awesome! I saw what you mean with the allow_other_hosts. I think it's a little wonky. For now let's just roll without that, and we can think about a nice way to handle something like that later if people want it.

Copy link
Member

@paulcsmith paulcsmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One tiny comment then LGTM! Thanks for tackling this!

src/lucky/redirectable.cr Outdated Show resolved Hide resolved
@paulcsmith
Copy link
Member

Looks great! Thank you 🎉

@paulcsmith paulcsmith merged commit 6a8132d into luckyframework:master Jun 8, 2020
@matthewmcgarvey matthewmcgarvey deleted the redirect-back branch June 8, 2020 22:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add redirect_back
3 participants