我有这个导轨助手_fields_answer.html.erb
<fieldset>
<div class="row">
<div class="col-xs-9">
<%= answerf.text_area :content, placeholder: "Answer", class: "form-control" %>
</div>
<%= answerf.hidden_field :question_number, value: qn %>
<div class="col-xs-2 buttonProjDel">
<%= answerf.hidden_field :_destroy %>
<%= link_to "remove", '#', class: "remove_fields btn btn-danger btn-xs buttonProjDelBut" %>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<% st = Time.now.strftime("%I%M%S%p") %>
<%= answerf.text_field :scale, id: 'slf'+st , type: 'text',
data: {'slider-id' => 'scale',
'slider-min' => '0',
'slider-max' => '10',
'slider-step' => '1',
'slider-value' => '5' } %>
</div>
</div>
<script>
$('<%= '#slf'+st %>').slider()
</script>
</fieldset>
我动态渲染这个助手,但我只能在第一个中使用滑块。所以我试着Id
为每个人做一个不同的人,但我做不到。使用上面的代码,我总是得到相同的结果Id
,即使使用Time
.
为什么使用的时间id
总是一样的?
还有另一种方法可以做到这一点吗?我想为每个答案制作一张幻灯片。
编辑:
呈现助手的代码:在projects.js.coffee
$(document).on 'click', 'form .add_fields', (event) ->
time = new Date().getTime()
regexp = new RegExp($(this).data('id'), 'g')
$(this).before($(this).data('fields').replace(regexp, time))
event.preventDefault()
在 application_helper.rb
def link_to_add_fields(name, f, association, locals)
new_object = f.object.send(association).klass.new
id = new_object.object_id
fields = f.fields_for(association, new_object, child_index: id) do |builder|
render("fields_" + association.to_s.singularize, answerf: builder, qn: locals[:qn])
end
link_to(name, '#', class: "add_fields " + locals[:class], data: {id: id, fields: fields.gsub("\n", "")})
end