marcamillion
marcamillion

Reputation: 33755

Devise gives me an 'email cannot be blank' error even though email is not blank

I am doing two things with my user registration with Devise.

  1. Two step confirmation - where all the user is required to do is to enter their email address at first.
  2. I have moved the registration/new form to my welcome#index view.

Even though I correctly entered the email address, and I see that it is included in the params hash in the log:

Started POST "/users" for 127.0.0.1 at 2012-06-05 20:35:43 -0500
Processing by Devise::RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"0qWg/RxG+XyDqdsdadww=", "email"=>"[email protected]", "commit"=>"Sign Me Up!"}
   (0.1ms)  begin transaction
  User Exists (0.2ms)  SELECT 1 FROM "users" WHERE LOWER("users"."email") = LOWER('') LIMIT 1
   (0.1ms)  rollback transaction
  Rendered devise/registrations/new.html.erb within layouts/application (9.2ms)
Completed 200 OK in 65ms (Views: 54.9ms | ActiveRecord: 0.0ms)

This is the error I am getting:

1 error prohibited this user from being saved:
Email can't be blank

My initial registration form looks like this:

<%= form_for(resource, :as => resource_name, :class => "send-with-ajax", :url => user_registration_path(resource)) do |f| %>
            <%= devise_error_messages! %>
              <%= f.email_field :email, :name => :email, :id => "form-email", :placeholder => "[email protected]", :input_html => {:autofocus => true} %>
              <%= f.submit :label => "Submit", :value => "Sign Me Up!"  %>
          <% end %>

Thanks.

Upvotes: 3

Views: 5081

Answers (2)

Anil
Anil

Reputation: 3919

Possible explanation:

Your email field is in the params hash as params[:email], while your create action in the controller probably is expecting it in params[:resource][:email]. When you remove the :name => :email assignment, then the email moves back to params[:resource][:email].

You have posted this hash:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"0qWg/RxG+XyDqdsdadww=", "email"=>"[email protected]", "commit"=>"Sign Me Up!"}

Can you see a difference in what it looks like when it works?

Upvotes: 4

marcamillion
marcamillion

Reputation: 33755

I just figured it out, it seems the :name attribute in my form helper for the email field was causing issues.

This was the original field:

<%= f.email_field :email, :name => :email, :id => "form-email", :placeholder => "[email protected]", :input_html => {:autofocus => true} %>

I changed it to:

<%= f.email_field :email, :id => "form-email", :placeholder => "[email protected]", :input_html => {:autofocus => true} %>

That fixed it, but I don't know why.

Upvotes: 1

Related Questions