【问题标题】:simple_form custom input and two fields with same idsimple_form 自定义输入和两个具有相同 id 的字段
【发布时间】:2013-12-11 11:10:43
【问题描述】:

我正在尝试使用 SimpleForm gem 为 datepicker 创建自定义输入

class DatepickerInput < SimpleForm::Inputs::Base
  def input
    out = ''
    out << @builder.text_field(attribute_name, input_html_options)
    out << @builder.hidden_field(attribute_name, { :class => attribute_name.to_s + "-alt"}) 
  end
end

但是当生成数据时,两个输入具有相同的 ids

<input class="datepicker" id="performance_cycle_start_date" name="performance_cycle[start_date]" type="text">
<input class="start_date_internal" id="performance_cycle_start_date" name="performance_cycle[start_date]" type="hidden">

如何将 id 更改为“#{original_id}_internal_format”之类的内容

附:此输入的代码取自http://www.chadcf.com/blog/jqueryui-datepicker-rails-and-simpleform-easy-way

【问题讨论】:

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


    【解决方案1】:

    正如博客所说,我们可以提供 ID,尝试如下。这将确保两个字段具有不同的 'id' 属性。

    class DatepickerInput < SimpleForm::Inputs::Base
      def input
        out = ''
        out << @builder.text_field(attribute_name, input_html_options)
        out << @builder.hidden_field(attribute_name, { :class => attribute_name.to_s + "-alt", :id => "#{attribute_name.to_s}_internal_format" }) 
      end
    end
    

    【讨论】:

    • attribute_name.to_s 仅返回字段名称,例如start_date,但原始 id 也有模型名称 - performance_review_start_date
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-29
    • 1970-01-01
    • 2019-04-18
    • 2014-10-14
    • 1970-01-01
    • 2020-05-19
    • 2014-07-17
    相关资源
    最近更新 更多