2016-12-13 72 views
0

我试图生成JavaScript的目的动态CSS的ID,下面的代码是给我的错误:麻烦与级联表格

<%= form_for @item, html: { multipart: true } do |f| %> 
    <%= f.fields_for :item_images do |builder| %> 
     <%= image_tag builder.object.image.url(:large), :class => "cropbox", 'data-id' => builder.object.id %> 
     <% for attribute in [:crop_x, :crop_y, :crop_w, :crop_h] %> 
      <%= builder.text_field attribute, :id => attribute + builder.object.id %> 
     <% end %> 
    <% end %> 
<% end %> 

我知道我不是串联属性和builder.object。身份证适当,但我已经尝试了一切都没有运气。我得到这个错误信息:

未定义的方法'+”为:crop_x:符号

感谢任何帮助,谢谢!

回答

1

您的预期结果是什么? 你能说出'''属性'''在你的表达中吗? 另外,你有没有试过.concat而不是'+'?

远离我的办公桌上,但我的建议是这样的: 例子:

attribute.concat(builder.object.id) 

也许需要attribute.to_i.to_s转换为响应型,或者创建一个不同的动态CSS ID解决方案

+0

预期的结果id应该像“crop_x01”等。属性来自循环,只有四个值:crop_x,:crop_y,:crop_w,:crop_h。感谢您的建议,我会尝试一下 – odinsride

+0

这有助于很多,我用它来让它工作 - 'attribute.to_s.concat(builder.object.id.to_s)'感谢您的帮助! – odinsride