rails partial rendering with uniq id

懵懂的女人 提交于 2019-12-11 14:23:56

问题


I'm trying to render a partial on a view when user is pressing a button, so actually it's a list of partials (the same partial is rendered more times) I use cocoon gem to render the partials like this:

<%= f.fields_for :features do |s| %>
  <%= render 'feature_fields', f: s %>
<% end %>
<div class="f-input for-button to-right">
  <%= link_to_add_association 'add more', f, :features, :class => "radius blue button" %>
</div>

and in my partial I have this code:

<% div_id = DateTime.now.to_i %>

so the idea is to have an uniq id for every rendered partial but my problem is the partials renders ok but the id is the same, the first id generated is applied to every partial


回答1:


Since you are using cocoon, using partial counter or any other modification in the partial itself would not help you.
This is because cocoon renders one template for new instances and uses this locally in the browser to generate new nested attribute blocks.

You can use cocoons callbacks to modify the generated block with JS.

See the cocoon readme.



来源:https://stackoverflow.com/questions/27420215/rails-partial-rendering-with-uniq-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!