formValidation jquery插件在rails上提交一个空白的设计注册表单

我正在为我的Devise注册表单使用formValidation查询插件。 该插件来自formValidation.io 。 我可以让表单注册有效或无效的条目,但它总是提交一个空白表格。 由于某种原因,表单显示它已填写,但我不断收到错误,说必填字段不能为空。 当我查看跟踪时,注册信息正在那里注册……但它在线路的某处丢失了。

跟踪:

Started POST "/users" for ::1 at 2015-05-09 20:11:21 -0700 source=rack-timeout id=b008bd12bcf5f1f2687f7e216cf42136 timeout=20000ms service=1750ms state=active Processing by Users::RegistrationsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"uzEtfaHhjFg5M1aRL6shfpMxA3/Wbf/CVm5R1cU+NDNjrOx2nbnlkfQ9t+SlvKTk2Lm+F1r1rkvLfauEtFKRtA==", "firstname"=>"fred", "lastname"=>"flintstone", "email"=>"wilma@bedrock.com", "pw1"=>"dino123", "pw2"=>"dino123", "commit"=>"Create Account"} (0.3ms) BEGIN User Exists (0.6ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('') LIMIT 1 (0.2ms) ROLLBACK Rendered users/registrations/_form.html.erb (5.9ms) Rendered users/registrations/new.html.erb within layouts/application (43.5ms) source=rack-timeout id=b008bd12bcf5f1f2687f7e216cf42136 timeout=20000ms service=2006ms state=active Rendered layouts/_sidebar_signedout.html.erb (0.5ms) Completed 200 OK in 237ms (Views: 206.9ms | ActiveRecord: 1.1ms) source=rack-timeout id=b008bd12bcf5f1f2687f7e216cf42136 timeout=20000ms service=2108ms state=completed source=rack-timeout id=4553e621abb4ef03750d71fd883738df timeout=20000ms state=ready source=rack-timeout id=4553e621abb4ef03750d71fd883738df timeout=20000ms service=0ms state=active 

形成

  resource_name, id: 'new_user', :url => registration_path(resource_name)) do |f| %>    'firstname', :class => 'form-control', :autofocus => true, :required => true, :placeholder => 'FIRST NAME' %> 'lastname', :class => 'form-control', :autofocus => true, :required => true, :placeholder => 'LAST NAME ' %> 'email', :class => 'form-control ', :autofocus => true, :required => true, :placeholder => 'YOUR EMAIL', :style=>"width:100%" %>  'form-control ', :name=>'pw1', :autofocus => true, :required => true, :placeholder => 'YOUR PASSWORD' %> 'pw2', :class => 'form-control ', :autofocus => true, :required => true, :placeholder => 'CONFIRM YOUR PASSWORD' %>  'btn btn-aqua btn-lg btn-block', :style => 'margin-bottom:5px' %>   $(document).ready(function() { $('#new_user').formValidation({ framework: 'bootstrap', icon: { valid: 'fa fa-check', invalid: 'fa fa-times', validating: 'fa fa-refresh' }, fields: { firstname: { validators: { notEmpty: { message: 'Please enter your first name' } } }, lastname: { validators: { notEmpty: { message: 'Please enter your last name' } } }, email: { validators: { notEmpty: { message: 'The email address is required' }, emailAddress: { message: 'The input is not a valid email address' } } }, pw1: { validators: { notEmpty: { message: 'The password is required' }, } }, pw2: { validators: { notEmpty: { message: 'Please confirm your password' }, identical: { field: 'pw1', message: 'The passwords do not match' } } } button: { // The submit buttons selector selector: '[type="submit"]:not([formnovalidate])', // The disabled class disabled: 'disabled' } } }); });  

您的问题是您在所有表单输入中设置了name属性。 不要那样做。

因为你用firstnamelastname等填充它们,你的params看起来像

 {"firstname"=>"fred", "lastname"=>"flintstone", "email"=>"wilma@bedrock.com", "pw1"=>"dino123", "pw2"=>"dino123", "commit"=>"Create Account"} 

什么时候应该看起来像

 {"user" => {"firstname"=>"fred", "lastname"=>"flintstone", "email"=>"wilma@bedrock.com", "pw1"=>"dino123", "pw2"=>"dino123" }, "commit"=>"Create Account"} 

Rails将自动填充那些user[firstname]user[lastname]等,这将使你的参数看起来像: user: { firstname: 'blabla', lastname: 'something'}

如果需要手动设置name ,并且代码需要具有属性的user对象,则需要将其写为name="user[attribute_name]"

Interesting Posts