2013-12-11 49 views
0

simple_form自定义输入和两个领域我试图使用SimpleForm宝石用相同的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"}) 
    end 
end 

创建日期选择器自定义输入,但时产生的数据都输入具有相同的ID

<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”

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

回答

0

由于博客说我们可以提供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 
+0

attribute_name.to_s只返回字段名称例如start_date,但原始ID也有型号名称 - performance_review_start_date – Vladimir