When a user clicks on a custom link, and the user is not logged in, I want to pre-populate the email field in the view for a custom login screen (value in the session). I’ve got a SessionsController method like below:
def new
# devise code seems to take this param, but it doesn't work
params[:sign_in] = { 'email' => session[:sharing_email] }
super
# This is too late as view is already rendered.
# I can put this in the view, but I'd rather do it in the controller
# resource.email = session[:sharing_email] if session[:sharing_email].present?
end
Should I just put this line in the view? It works fine.
resource.email = session[:sharing_email] if session[:sharing_email].present?
Is there any good way to get this into my controller before I call super? Note, resource does not exist before the call to super.
You can copy the code from the Devise SessionsController and paste it there, instead of using super, that way you can insert lines of code exactly where you mean to.
And since what you really want is not to call super, since you want to alter it’s behavior, it might not be such a bad idea. It might be a case where you have duplication of code but not duplication of logic.
So far I haven’t changed the setting of the value in the view, as that’s simple and works, although I can see an argument that code like this does not belong in the view:
resource.email = session[:sharing_email] if session[:sharing_email].present?