How to set default value for email in devise login form

I’m using Devise 3.1.1 and Rails 4

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.

From the perspective of long term maintenance, can does it makes sense to be calling super rather than copying the code and altering it?

Another option would be to put this before calling super (mistake in my previous sample):

params[resource_name.to_sym][:sign_in] = { 'email' => session[:sharing_email] }

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?

Stack Overflow discussion on this topic