Ruby on rails 设计给了我一个';电子邮件不能为空';错误,即使电子邮件不是空的
我在Desive的用户注册中做了两件事Ruby on rails 设计给了我一个';电子邮件不能为空';错误,即使电子邮件不是空的,ruby-on-rails,devise,Ruby On Rails,Devise,我在Desive的用户注册中做了两件事 -要求用户首先输入其电子邮件地址 我已将注册/新表单移至我的welcome#index视图 即使我正确输入了电子邮件地址,并且我看到它包含在日志中的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: {"utf
welcome#index
视图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 %>
resource_name,:class=>“使用ajax发送”,:url=>user_registration_path(资源))do | f |%>
:email,:id=>“表单电子邮件”,“占位符=>”您的-email@address.com“,:input_html=>{:autofocus=>true}%>
“提交”,:value=>“注册我!”%>
谢谢。我刚想出来,我的表单助手中的电子邮件字段的
:name
属性似乎引起了问题
这是原始字段:
<%= f.email_field :email, :name => :email, :id => "form-email", :placeholder => "your-email@address.com", :input_html => {:autofocus => true} %>
:email,:id=>“表单电子邮件”,“占位符=>”您的-email@address.com“,:input_html=>{:autofocus=>true}%>
我把它改成:
<%= f.email_field :email, :id => "form-email", :placeholder => "your-email@address.com", :input_html => {:autofocus => true} %>
“表单电子邮件”,:占位符=>“您的-email@address.com“,:input_html=>{:autofocus=>true}%>
这解决了问题,但我不知道为什么。可能的解释:
您的电子邮件字段作为params[:email]
散列在params[:email]中,而您在控制器中的创建操作可能在params[:resource][:email]
中需要它。当您删除:name=>:email
分配时,电子邮件将移回参数[:resource][:email]
您已发布此哈希:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"0qWg/RxG+XyDqdsdadww=", "email"=>"abc@test.com", "commit"=>"Sign Me Up!"}
你能看出它工作时的不同吗?你说得绝对正确<代码>参数:{“utf8”=>“✓", "真实性令牌“=>”0qWg/RxG+Xkjhgjsdsxaxcu/RL1yuBgtiVN1fw=“,”用户“=>{”电子邮件“=>”abc@test.com“},“提交”=>“注册!”}