I'm learning rails right now. Today for some odd reasons I put the line of flash after the redirect, which is obviously a bad practice. But strangely, after the redirect the flash works.
Isn't flash supposed to be used to carry the message to the next request?
Bearing this in mind I added a line that is supposed to makes the program stop for 10 seconds after the redirect. Then I noticed that the entire website will hold for 10 seconds before redirect. Why did redirect_to method wait 10 seconds? This will not be the case if I replace the redirect_to method with render. I've put the code blocks below.
redirect_to root_url
message = "Account not activated. "
message += "Check your email for the activation link."
flash[:danger] = message #can still see the flash after redirect
Entire Controller:
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
if user.activated?
log_in user
params[:session][:remember_me] == '1' ? remember(user) : forget(user)
redirect_back_or user
else
redirect_to root_url
sleep 10
message = "Account not activated. "
message += "Check your email for the activation link."
flash[:danger] = message
end
else
flash.now[:danger] = "Invalid email/password combination"
render "new"
end
end
def destroy
log_out if logged_in?
redirect_to root_url
end
end
Before Rails does anything with your controller method it reads and builds the response.
If you were to return immediately after calling redirect_to
your flash wouldn't be captured and inserted into the appropriate methods.