Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4 + Devise: Keep form fields filled after error

I have a rather long user registration form and was hoping to find a way to retain form values after an error. I came across a similar question, but the answer does not seem to work, perhaps because its from 2010. Is there any simple way to accomplish this? Here's my relevent controller code (mostly Devise):

def create
  if params[:user] && !params[:user][:password]
    params[:user][:password] = "testpassword"
    build_resource(sign_up_params)

    resource.save
    yield resource if block_given?
    if resource.persisted?
      if resource.active_for_authentication?
        set_flash_message! :notice, :signed_up
        sign_up(resource_name, resource)
        respond_with resource, location: after_sign_up_path_for(resource)
      else
        set_flash_message! :notice, :"signed_up_but_#{resource.inactive_message}"
        expire_data_after_sign_in!
        respond_with resource, location: after_inactive_sign_up_path_for(resource)
      end
    else
      # I believe this is where errors are handled
      clean_up_passwords resource
      set_minimum_password_length
      respond_with resource
      #  render :action => "new" I tried adding this line instead of the line above with no success
    end
  else
    super
  end
end
like image 204
Jeremy Thomas Avatar asked Sep 12 '26 12:09

Jeremy Thomas


1 Answers

Your params are passing to the view when you render. so, something like this:

f.email_field :email, value: params.include?(:user)? params[:user][:email] : ""

should work.

like image 103
Guido Avatar answered Sep 14 '26 05:09

Guido