我在Rails中有一个simple_form_for
。一些字段在模型中有验证。如果验证失败,我希望这可以防止提交表单。这不会发生,因为我现在编码了。
事物模型:
validates :name, presence: true
表格:
<%= simple_form_for @thing, html: {class: 'form-horizontal' } do |f| %>
<%= f.error_notification %>
...
<%= f.input :name, label: "Name"%>
....
<div class="modal-footer">
<%= link_to 'Cancel', [@thing], class: 'btn' %>
<button class="btn btn-primary" data-disable-with="Saving...">Save</button>
</div>
我尝试将按钮更改为f.submit
但似乎没有更改功能。我做错了什么?
编辑:这是我的控制器创建方法:
def create
@thing = Api::Thing.new(params[:api_thing])
if @thing.valid?
@thing.active = @thing.active == 'true'
if @thing.save
flash[:notice] = 'Thing was successfully created.'
respond_with @thing
else
flash[:error] = @thing.message
render 'new'
end
else
render 'new'
end
end