ruby-on-rails 简单表单不向控制器RAILS传递参数

jckbn6z7  于 7个月前  发布在  Ruby
关注(0)|答案(1)|浏览(63)

我有一个简单的表单,里面有一些JavaScript,它根据用户的选择显示一些输入字段或其他字段。我得到的奇怪行为是,无论我把div放在顶部(例如div id="id1"),最后一个f.input(:motivo)都不会传递给控制器。如果我改变div的位置,那么现在另一个f.input :motivo不会传递。

<%= simple_form_for(@vacacion, validate: true, html: { class: "form-horizontal", role: "form" }) do |f| %>
  <%= f.error_notification %>
  <%= display_base_errors @vacacion %>

  <%= f.input :tipo, label: 'Tipo', as: :select, collection: [["Registrar días de vacaciones utilizados", "tomadas"], ["Abonar días de vacaciones", "abonadas"]], required: true %>

  <div id="id1" style="display:none">
    <%= f.input :desde, label: 'Fecha inicio vacaciones', as: :datepicker, input_html: { value: @vacacion.desde.present? ? l(@vacacion.desde, format: :slash) : nil } %>
    <%= f.input :hasta, label: 'Fecha fin vacaciones', as: :datepicker, input_html: { value: @vacacion.hasta.present? ? l(@vacacion.hasta, format: :slash) : nil } %>
    <%= f.input :dias_tomados, label: 'Días de vacaciones utilizados', input_html: { min: 0 }, hint: '<div id="dias_habiles_div"></div>'.html_safe %>
    <%= f.input :motivo, input_html: { id: 'motivo_tomadas'} ,label: 'Motivo de los días tomados' %>
  </div>

  <div id="id2" style="display:none">
    <%= f.input :dias_abonados, label: 'Días de vacaciones a abonar', input_html: { min: 0 } %>
    <%= f.input :motivo, input_html: { id: 'motivo_abonadas'},label: 'Motivo de los días abonados' %>
  </div>

...

$(document).ready(function(){
    $("#vacacion_tipo").change(function(){
      if(this.value == "tomadas"){
        $("#campos_vacaciones_abonadas").hide("slow");
        $("#campos_vacaciones_tomadas").show("slow");
      }

      if(this.value == "abonadas"){
        $("#campos_vacaciones_abonadas").show("slow");
        $("#campos_vacaciones_tomadas").show("slow");
      }
    });

字符串
我尝试在每种情况下为输入添加id,但这没有帮助。当我查看控制台检查参数时,每个其他参数都传递正常,但使用'motivo'我得到"motivo"=>""
有什么建议吗?谢谢

rqdpfwrv

rqdpfwrv1#

这实际上不是Rails或SimpleForm的问题,这只是表单的工作方式。
查看生成的HTML,您将看到有两个名为“motivo”的输入。name属性是驱动表单提交的参数的属性。由于不能有两个同名的参数,因此它只提交遇到的最后一个参数,有效地覆盖了第一个参数。

相关问题