rails简单表单以进行验证

时间:2015-10-19 22:08:53

标签: ruby-on-rails forms validation

我在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

0 个答案:

没有答案