rails 4简单形式include_blank仍然发送空属性

时间:2016-03-17 21:46:58

标签: ruby-on-rails simple-form modelattribute

我有以下表单设置:

= simple_form_for(@job, url: job_payment_path, html: { id: 'payment-processor-form' }) do |j|
  div[class='row']
    div[class='col-md-12']
      div[class='panel panel-default']
        div[class='panel-heading']
          h3[class='panel-title']
            |Total Cost
        div[class='panel-body']
          h2[class='job-cost' data-initial = "#{job_base_price}"]
            = number_to_currency(job_base_price)
        div[class='panel-heading']
          h3[class='panel-title']
            |Have a coupon?
        div[class='panel-body']
          div[class='row-inline']
            div[class='row-block row-block-one']
              = j.simple_fields_for :coupon_attributes, @job.coupon do |c|
                = c.input_field :code, maxlength: 50, id: 'coupon-code', class: 'form-control', data: { 'initial' => 0 }, include_blank: false
            div[class='row-block']
              button[type='button' class='btn btn-primary' id='coupon-verify' ]
                |Verify
            p[class='help-hint']
              = t('simple_form.hints.coupon.code')

  div[class='row']
    div[class='col-md-12']
      = j.button :button, type: 'button', class: 'btn-primary text-uppercase', id: 'purchase-job' do
        = job_posting_button_step_label

当此表单提交时,我看到以下属性:

enter image description here

这是不对的。

我希望优惠券代码为零,而不是“”。

我在这里错过了什么吗?

1 个答案:

答案 0 :(得分:0)

我看到两件事:

第一:
:include_blank是选择字段的选项,它会在开头跳过生成空白option标记。 code是输入字段。如果要强制输入值,请尝试required: true

第二:
表单值以application/x-www-form-urlencoded的形式发送到服务器。空的输入字段作为ie job[coupon_attributes][code]=发送。空字符串与无值之间没有区别 Rails中的约定是将所有输入值解释为字符串(以及稍后在为模型赋值时进行类型转换)。因此,空值始终返回为''