Ruby on rails 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
<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>
添加新客户
如您所见,我正在表单元素上使用引导样式。当我提交表格时,我希望发生以下情况:
如果有人有任何想法,或能帮我解开这些文件的神秘面纱,我将不胜感激 在模型(特别是客户模型)中使用验证,这将在数据保存到数据库之前发生。见文件 例如:
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?
。