In addition to the normal login route (UserSessionsController.create) I have a special route in a different controller that does a bunch of other stuff including creating a new user and I would like to be able to log in as that newly created user. To run this route you have to already be logged in, so I would like to destroy the current session and create a new session with the new user.
Pulling from the existing routes that work I have put together this code:
@user_session = UserSession.find
@user_session.destroy
@user_session = UserSession.new({email: new_user.email, password: new_user.password})
@user_session.save!
redirect_to value_chain_hierarchy_path(new_business_entity)
Because the user was just created, we still have access to their password. Stepping through this code with a debugger I can see that the current session gets destroyed (has a nil user) and when I save the new session and ask for its user I get the new user back, but when the page loads I'm still logged in as the previous user.
I'm pretty stumped. I'm guessing it has something to do with updating the session cookies or something like that. But I don't know what to try next.