formValidation jquery插件在rails上提交一个空白的设计注册表单 [英] formValidation jquery plugin submitting a blank devise registration form on rails

查看:200
本文介绍了formValidation jquery插件在rails上提交一个空白的设计注册表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using the formValidation query plugin for my Devise registration form. The plugin is from formValidation.io . I can get the form to register valid or invalid entries, but it always submits a blank form. For some reason the form is showing that it's filled out, but I keep getting errors saying that the required fields can't be blank. When I look at the trace, the sign up info IS being registered there...but it's getting lost somewhere along the line.

trace:

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

form

<%= 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 所有表单输入中的属性。不要这样做。

Your issue is that you're setting the name attribute in all your form inputs. Don't do that.

由于您填写了 firstname lastname 等你的参数看起来像

Since you filled them with firstname, lastname, etc. your params look like

{"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] 等,这将使你的params看起来如下所示: user:{firstname:'blabla',lastname:'something'}

Rails will automatically fill those with, for example, user[firstname], user[lastname], etc. which will make your params look something like: user: { firstname: 'blabla', lastname: 'something'}

手动设置名称,并且您的代码期望具有属性的用户对象,则需要将其写为 name =user [attribute_name]

If you need to manually set name, and your code expects a user object with attributes, you need to write it as name="user[attribute_name]"

这篇关于formValidation jquery插件在rails上提交一个空白的设计注册表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆