我有一个窗体,它具有fields_for
循环遍历的多个记录。每条记录使用打开的模式,然后用于添加表单条目。我的问题是,为了打开模式,我需要使用唯一的标识符。在创建“主”表单记录之前,我没有ID可以用作唯一标识符。我尝试使用初始化的版本,该版本是唯一的,但不会打开模型。我认为是因为初始化的记录中有一个#
...
创建记录时,我有这个工作,因为那样我就可以使用ID。但是这是初始化记录的问题:
我的控制器:
@shop_product = ShopProduct.find_or_initialize_by()
表格总结:
<%= form_for @shop_product do |f| %>
<%= f.fields_for :shop_product_print_files do |ff| %>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal-<%= "#{ff.object.print_location.id}" %>_<%= "#{@shop_product}" %>" id="printLocationId_<%= "#{ff.object.print_location.id}" %>(<%="#{ff.object.print_location.id}"%>)">
Choose Image
</button>
<div class="modal fade" id="myModal-<%= "#{ff.object.print_location.id}" %>_<%= "#{@shop_product}" %>" tabindex="-1" role="dialog" data-keyboard="false">
</div>
<% end %>
<%= f.submit "Sync" %>
<% end %>
转换为:http : //jsfiddle.net/a01h6rz7
^这显示了按钮和模式之间的关系。模态有很多,因此我需要一个独特的模态,因此模态会打开正确的关联。
创建记录后,就可以使用@shop_product.id
它了。有什么方法可以@shop_product
在新产品上使用,以获得可以正常工作的唯一标识符吗?
我标记了javascript,以防万一可能有基于@shop_product
初始化创建唯一标识符的想法或方法。如果有更多信息想帮助解决,请告诉我
试试这个代码
<%= form_for @shop_product do |f| %>
<% shop_product_id = @shop_product.object_id %>
<%= f.fields_for :shop_product_print_files do |ff| %>
<% print_location_id = ff.object.print_location.id%>
<button
type="button"
class="btn btn-primary"
data-toggle="modal"
data-target="#myModal-<%= print_location_id %>_<%= shop_product_id %>"
id="printLocationId_<%= print_location_id %>(<%= print_location_id %>)"
>
Choose Image
</button>
<div
class="modal fade"
id="myModal-<%= print_location_id %>_<%= shop_product_id %>"
tabindex="-1"
role="dialog"
data-keyboard="false">
</div>
<% end %>
<%= f.submit "Sync" %>
<% end %>
@shop_product
因为多数民众赞成在一个对象。如果您需要参考并且还没有ID,object_id
将为您提供一个唯一的标识符。print_location
则不需要进行ff.objec.print_location.id
查询print_location
。改为使用print_location_id
。<%= "#{ff.object.print_location.id}" %>
而要使用简单的erb代码,例如<%= ff.object.print_location.id %>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句