即使电子邮件不是空白,Devise也会给我一个“电子邮件不能为空”错误

时间:2012-06-06 03:49:57

标签: ruby-on-rails devise

我正在用Devise进行用户注册时做两件事。

  1. Two step confirmation - 要求所有用户首先输入他们的电子邮件地址。
  2. 我已将注册/新表单移至welcome#index视图。
  3. 即使我正确输入了电子邮件地址,我也看到它包含在日志中的params哈希中:

    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"=>"abc@test.com", "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)
    

    这是我得到的错误:

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

    我的初始注册表格如下:

    <%= 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 => "your-email@address.com", :input_html => {:autofocus => true} %>
                  <%= f.submit :label => "Submit", :value => "Sign Me Up!"  %>
              <% end %>
    

    感谢。

2 个答案:

答案 0 :(得分:4)

可能的解释:

您的电子邮件字段位于params哈希params[:email]中,而您在控制器中的创建操作可能会在params[:resource][:email]中预期。删除:name => :email分配后,电子邮件将移回params[:resource][:email]

您已发布此哈希:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"0qWg/RxG+XyDqdsdadww=", "email"=>"abc@test.com", "commit"=>"Sign Me Up!"}

你能看到它工作时的样子有什么不同吗?

答案 1 :(得分:1)

我只是想通了,似乎电子邮件字段的表单助手中的:name属性导致了问题。

这是原始字段:

<%= f.email_field :email, :name => :email, :id => "form-email", :placeholder => "your-email@address.com", :input_html => {:autofocus => true} %>

我把它改为:

<%= f.email_field :email, :id => "form-email", :placeholder => "your-email@address.com", :input_html => {:autofocus => true} %>

修正了它,但我不知道为什么。