簡體   English   中英

formValidation jQuery插件在Rails上提交空白的devise注冊表格

[英]formValidation jquery plugin submitting a blank devise registration form on 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

形成

<%= 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屬性。 不要那樣做

因為您用firstnamelastname等填充了它們,所以參數看起來像

{"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]"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM