我正在为我的设计注册表格使用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
形式
<%= simple_form_for(resource, :as => resource_name, id: 'new_user', :url => registration_path(resource_name)) do |f| %>
<% if resource.errors.any? %>
<%= devise_error_messages! %>
<% end %>
<%= f.text_field :firstname, :name=>'firstname', :class => 'form-control', :autofocus => true, :required => true, :placeholder => 'FIRST NAME' %>
<%= f.text_field :lastname, :name =>'lastname', :class => 'form-control', :autofocus => true, :required => true, :placeholder => 'LAST NAME ' %>
<%= f.email_field :email, :name=>'email', :class => 'form-control ', :autofocus => true, :required => true, :placeholder => 'YOUR EMAIL', :style=>"width:100%" %>
<%= f.password_field :password, :class => 'form-control ', :name=>'pw1', :autofocus => true, :required => true, :placeholder => 'YOUR PASSWORD' %>
<%= f.password_field :password_confirmation, :name=>'pw2', :class => 'form-control ', :autofocus => true, :required => true, :placeholder => 'CONFIRM YOUR PASSWORD' %>
<%= f.submit 'Create Account', :class => 'btn btn-aqua btn-lg btn-block',
:style => 'margin-bottom:5px' %>
<%end%>
<script>
$(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'
}
}
});
});
</script>
您的问题是您要在所有表单输入中设置name
属性。不要那样做。
由于您用firstname
,lastname
等填充了它们。您的参数看起来像
{"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]"