抓取用户为option_for_select选择的内容

时间:2016-02-11 17:46:10

标签: ruby-on-rails ruby haml

我有下表enter image description here

Rate Type中option_for_select的两个选项是“Flat”或“Percentage”。我的盒子默认为“Flat”。

问题:我无法找到用户最终选择的内容。如果他们选择百分比我就不会知道。我怎样才能知道他们选择了什么价值?我想知道,因为如果他们选择“Flat”,我希望Institutional框中的货币格式为真。

= form_for @payer_contract,:html => {:id => "add-payer-contract-form"} do |f|
  %table.table-base.table-striped{:id => "case-global-table"}
    %tbody
      .clone-wrapper
        - @payer_contract.phase_dates.each.with_index do |pd, index|
          =fields_for "phase_dates",pd do |p|
            %tr.clone-target{:id => 'phase-table-row', :name => 'payer_contract[phase][:i][desc]'}
              - @phase_name = t('payer_contracts.phase')+ ' ' +(index+1).to_s
              %td
                %input.field-input{type: 'text', readonly: 'readonly', placeholder: 'Phase', value: @phase_name}
              %td
                = p.select :rate_type, options_for_select(@rate_type, @selected_rate_type), :name=> "phase_dates[rate_type]-c#{index}", required: true, :style => 'width:125px'
              %td
                = p.text_field :institutional, :name=> "phase_dates[institutional]-c#{index}", class: 'field-input', 'currency-format' => (@rate_type == 'Flat')
              %td
                = p.text_field :professional, :name=> "phase_dates[professional]-c#{index}", class: 'field-input'
              %td
                %button.icon-fallback-text.clone-add{"aria-label" => "Add", :type => "button"}
                  %span.icon-add-square{"aria-hidden" => "true"}
                %button.icon-fallback-text.clone-remove{"aria-label" => "Remove", :disabled => @payer_contract.phase_dates.size > 1 ? false : true, :type => "button"}
                  %span.icon-remove-square{"aria-hidden" => "true"}

在上面的代码中,Institutional始终显示货币格式,即使我选择“百分比”,因为@rate_type由于某种原因从未找到用户实际选择的内容。

当我在Rate Type下检查“Flat”框2时,我发现:

<select id="phase_dates_rate_type-c1" name="phase_dates[rate_type]-c1" u-index="1" aria-invalid="false" class="valid"><option value="

有什么想法吗?

控制器看起来像这样:

def new

@payer_contract = PayerContract.new(
     id: '',
     description: '',
     type_code: {
         id: '',
         code_key: '',
         display: '',
         code_group: ''
},
     base_contract_rate: '',
     phase_dates: [{
                       id: '',
                       rate_type: '',
                       institutional: '',
                       professional: ''
                   }],
     donor_claim_phase: '',
)
@selected_rate_type = 'Flat'

1 个答案:

答案 0 :(得分:0)

p.text_field :institutional中,您想要比较的值是pd[:rate_type]吗?看起来@rate_type是两个选项的数组。

对于已保存的记录,您可以尝试:

= p.text_field :institutional, :name=> "phase_dates[institutional]-c#{index}", class: 'field-input', 'currency-format' => (pd[:rate_type] == 'Flat')

对于您使用javascript动态添加的表单元素,您必须在javascript中切换费率类型。