0

Got this in my view

 <% elsif ["radio", "checkbox"].include?(part_child.display_type) %>  
   <%=   tick_tag_slice  part_child%>
 <% end %>

calling tic_tag_slice from the helper.

below is my helper of the same

  def tick_tag_slice(part_child)
render_type = part_child.display_type == "radio" ? "radio_button_tag" : "check_box_tag"
html = ""
part_child_option = parts_position(part_child.options)

part_child_option.  each_slice(6) do |o|
  html += "<div class = 'row'>"
  html += "<div class='col-sm-2 uno_part_wrapper'>"
  html += "<label class = 'p_name' for='#{attr_name}'>"
    "data-part-type" =>"#{part_child.display_type}",
    html += image_tag o.photo(:small), class: "tick_option_img",
        "data-option-name" => o.name
      html += "</label>"
      html += "</div>"
      html += "</div>"

  end
    html.html_safe
end

Here I am trying to run each loop after every col-sm-2, I am getting thi s error when I am trying to run the app.

undefined method `id' for Array:0x007f0e282d61c0>

The error line it shows in the view where there's nothing as id.

Tried to get tthis but couldn't get it working

Please help me figutr this out.

Community
  • 1
  • 1
Suraj
  • 2,423
  • 12
  • 39
  • 75

1 Answers1

0

Array#each_slice returns an array of results, so your o variable yielded to the block needs to be eached over again to give your six entries.

From the docs on Enumerable:

(1..10).each_slice(3) { |a| p a }
# outputs below
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
[10]

I don't see id referenced in the code above but there may be some method relying on o being a single object there.

Peter Mellett
  • 542
  • 5
  • 18
  • It's `o.photo(:small)` - `o` is an array here, not an object. – Marcus Ilgner Aug 27 '15 at 11:19
  • That doesn't make sense. `each_slice` returns the enumerable split into enumerables of that size, so inside that loop you would then need to `#each` the `o` variable to get your actual object with the `photo` method. – Peter Mellett Aug 27 '15 at 11:27
  • If id is not referenced how come I am getting that error. – Suraj Aug 27 '15 at 11:32
  • Got it. I was missing a loop to generate 6 `col-sm-2` No idea about that ID refrencing though. Thanks peter – Suraj Aug 27 '15 at 11:47