Chantal
Chantal Software developer focused on personal development not only as a coder but as a human being.

Did you know you could pass a method to a redirect?

Did you know you could pass a method to a redirect?

This post is going to be super short. The other day I discover that you can actually pass a method to a redirect_to in rails and I thought I should share.

A little background

I was working on a bug where after the user changes the password successfully instead of being redirected to where he was when he began the transaction he would stay on the change_password page which it didn’t make much sense.

After digging around I found that the User’s profile changes according to the type of account they have which is pretty cool. so it made sense to send them back to wherever they started. At work we already have a method that directs the user according to the type of account they have.

Super cool redirect

This is a piece of code extracted from an update action.

    if password_changed
      track_change_password_success
      set_flash(:success, 'Your password was changed successfully')
      redirect_to account_summary_router
    end

Here account_summary_router is the method in charge of redirecting the user according to the type of account, they customer/user sometimes they have multiple accounts, loans, usernames, etc.

I thought this is pretty cool and I discover this by meer luck.

I hope you can do this too in your code.

comments powered by Disqus