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

url_after_create does not work as expected #793

Closed
monicao opened this issue Aug 31, 2018 · 4 comments
Closed

url_after_create does not work as expected #793

monicao opened this issue Aug 31, 2018 · 4 comments

Comments

@monicao
Copy link

monicao commented Aug 31, 2018

Hi there!

I am trying to override where the user is redirected after signing up, so according to the docs I defined a method called url_after_create, but the controller still redirects :back.

When I override the method for url_after_destroy the redirect works as expected.

Looking at the code it seems that the problem is the method is using redirect_back_or url_after_create instead of redirect_to url_after_create

https://github.com/thoughtbot/clearance/blob/v1.16.0/app/controllers/clearance/sessions_controller.rb#L25

I am using v1.16.0, but this code is the same in 1.16.1.

If this is indeed a bug, and not a misunderstanding of the docs on my part, I am happy to submit a PR.

@emilford
Copy link

Hi @monicao,

Did you override the url_after_create method definition on the correct controller? Changing where the user is redirected to after signing up is handled in users_controller.rb, but you linked to the url_after_create method definition in sessions_controller.rb.

Otherwise, redirect_back_or url_after_create looks correct. It will redirect to the url returned by url_after_create unless there is a return_to url set in the session.

Hope this helps.

@monicao
Copy link
Author

monicao commented Oct 27, 2018

Hi @emilford,

Thanks for your response!

Ooops, I mistyped. I meant to say "I am trying to override where the user is redirected after logging in."

My understanding is that if you define a method called url_after_create in the sessions_controller, it will change where the user is redirected after logging in.

This doesn't seem to work in version 1.16.1.

Please see this sample app and the sessions controller test case: https://github.com/monicao/clearance_test

@emilford
Copy link

emilford commented Oct 27, 2018

Hi @monicao,

Ah, I see the issue. Clearance's default routes route sign in requests to clearance/sessions#create. In your sample app, you can see this if you run bundle exec rake routes.

So while you've created a SessionsController, and have correctly inherited from Clearance::SessionsController and overridden the def url_after_create method, the controller is never reached.

If you update your routes to override the sign in action to instead route to the SessionsController, your test passes and you have your desired behavior. resource :session, controller: "sessions", only: [:create]

This isn't entirely obvious from the documentation. I'm sure thoughtbot would welcome a PR that updates the documentation to clarify this for future users.

@mjankowski
Copy link
Contributor

I'm going to close this on the assumption that the last comment resolves this issue for you. Please re-open if that's not the case.

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

No branches or pull requests

3 participants