Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails中带引导的简单表单验证_Ruby On Rails_Twitter Bootstrap_Simple Form - Fatal编程技术网

Ruby on rails Rails中带引导的简单表单验证

Ruby on rails Rails中带引导的简单表单验证,ruby-on-rails,twitter-bootstrap,simple-form,Ruby On Rails,Twitter Bootstrap,Simple Form,我的rails应用程序中有以下简单的表单: <div class="panel panel-default"> <div class="panel-heading"> <h3 class="panel-title center">Add New Customer</h3> </div> <div class="panel-body"> <%= simple_form_for(@customer, ht

我的rails应用程序中有以下简单的表单:

<div class="panel panel-default">
 <div class="panel-heading">
  <h3 class="panel-title center">Add New Customer</h3>
 </div>
  <div class="panel-body">
   <%= simple_form_for(@customer, html: {class:'form-horizontal'}, wrapper: :horizontal_form) do |f| %>
    <%= f.input :first_name, input_html: {class:'form-control'} %>
    <%= f.input :last_name, input_html: {class:'form-control'} %>
    <%= f.input :phone_number, as: :tel, input_html: {class:'form-control'} %>
    <%= f.input :email_address, as: :email, input_html: {class:'form-control'} %>
    <%= f.input :address, input_html: {class:'form-control'} %>
    <%= f.input :city, input_html: {class:'form-control'} %>
    <%= f.input :postal_code, input_html: {class:'form-control'} %>
    <%= f.input :customer_type, collection: ["Retail", "Contractor", "Dealer"], input_html: {class:'form-control'}, prompt: "Select Customer Type" %>
    <br />
    <%= f.button :submit, "Create Customer", class: "col-md-3 bump-right" %>
   <% end %>
  </div>
</div>

添加新客户

如您所见,我正在表单元素上使用引导样式。当我提交表格时,我希望发生以下情况:

  • 验证电子邮件
  • 验证电话号码
  • 需要所有字段
  • 目前,当我提交表格时,上述三种情况都没有发生。查看文档中的simple_form(),我不知道我需要做什么来实现最终结果。我曾尝试为每个输入添加f.error字段,但似乎没有任何效果

    有一个2年前的问题:-但这对我来说是陌生的,考虑到2.5年前的版本,我确信有些东西已经改变了


    如果有人有任何想法,或能帮我解开这些文件的神秘面纱,我将不胜感激

    在模型(特别是客户模型)中使用验证,这将在数据保存到数据库之前发生。见文件

    例如:

    class Person < ActiveRecord::Base
      validates :name, presence: true
    end
    
    Person.create(name: "John Doe").valid? # => true
    Person.create(name: nil).valid? # => false
    
    class-Person真的
    Person.create(名称:nil).有效吗?#=>假的
    
    您也不需要显式调用
    create
    。您只需调用
    Person.new(名称:“whatever”).valid?