【问题标题】:rails 4 simple form include_blank still sending empty attributesrails 4 simple form include_blank 仍然发送空属性
【发布时间】:2016-07-04 11:51:27
【问题描述】:

我有以下表单设置:

= 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

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

这是不对的。

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

我错过了什么吗?

【问题讨论】:

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


    【解决方案1】:

    我看到两件事:

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

    秒:
    表单值作为application/x-www-form-urlencoded 发送到服务器。空输入字段以job[coupon_attributes][code]= 的形式发送。空字符串和没有值没有区别。
    Rails 中的约定是将所有输入值解释为字符串(稍后在将值分配给模型时进行类型转换)。所以空值总是返回为''

    【讨论】:

      猜你喜欢
      • 2015-11-09
      • 1970-01-01
      • 1970-01-01
      • 2021-10-17
      • 1970-01-01
      • 2014-01-04
      • 1970-01-01
      • 1970-01-01
      • 2017-05-03
      相关资源
      最近更新 更多