验证Rails表单提交

时间:2014-05-13 07:26:28

标签: javascript jquery ruby-on-rails forms validation

我正在尝试在表单提交之前验证表单,但表单已提交并且jQuery部分未被调用。

<%= form_for(:session,class:"form-signin form-horizontal",:id=>
  "form",:role=>
  "form") do |f| %>
  <div class="input-group input-group-mardin-bottom">
    <span class="input-group-addon">
      <i class="glyphicon glyphicon-user">
      </i>
    </span>
    <%= f.text_field :email,class:"form-control required",:placeholder=>
      "username or email", name:"username"%>
    </div>
    <div class="input-group input-group-mardin-bottom">
      <span class="input-group-addon">
        <i class="glyphicon glyphicon-lock">
        </i>
      </span>
      <%= f.password_field :password,class:"form-control required",:placeholder=>
        "password", password:"password" %>
      </div>
      <div class="input-group">
        <div class="checkbox">
          <label>
            <input id="login-remember" type="checkbox" name="remember" value="1">
            Remember me
          </label>
        </div>
      </div>
      <div class="form-group login-page-margin-10">
        <div class="col-sm-12 controls">
          <%= f.submit "Sign In", :class =>
            "btn btn-success" %>
          </div>
        </div>
        <div class="form-group">
          <div class="col-md-12 control">
            <hr/>
            <div>
              <a href="#">
                <strong>
                  Forgot your password?
                </strong>
              </a>
            </div>
          </div>
        </div>
        <%end%>

的jQuery

 $("#form").validate({

});

根本没有调用jQuery validate方法。

3 个答案:

答案 0 :(得分:0)

它在rails 3中,但在你的动作中预加载@session对象并尝试

<%= form_for @session , html: { id: "form", class: "form-signin form-horizontal", role: "role" } do |f| %> 
<% end %>

答案 1 :(得分:0)

这个示例可能会对您有帮助。请与您的form一起尝试。

$(document).ready(function () {
$(“#new_user”).validate({
debug: true,
rules: {
“user[email]“: {required: true, email: true},
“user[password]“: {required: true, minlength: 6},
“user[password_confirmation]“: {required: true, equalTo: “#user_password”}
}
});
});

<强> Source

希望它有所帮助!

答案 2 :(得分:0)

您是否安装了jquery validate插件?

如果没有,你可以使用这个gem:

gem "jquery-validation-rails"

如果您正在使用资产管道,请将以下内容添加到您的application.js文件中:

//= require jquery.validate
//= require jquery.validate.additional-methods