0

Below is my loop I am running in a rails helper method.

part_child_option.each do |o|

  html += "<div class='col-sm-2 uno_part_wrapper'>"
  html += "<label class = 'p_name' for='#{attr_name}'>"

    html += image_tag o.photo(:small), class: "tick_option_img",
      html += "</label>"
      html += "</div>"
    end
    html.html_safe
end

I have a problem now. class col-sm-2 is there, so It is under a class row in my view. The row is outside of my helper and hence I am not able to loop it.

The pointed solution doesn't have such scenario

Now I want my row class to close once the col-sm-2 is done with six times. And then I want to start the class row again and everything same as above in my class row.

I hope the question is clear now.

How can this be done.

Suraj
  • 2,423
  • 12
  • 39
  • 75
  • 2
    possible duplicate of [Rails each loop insert tag every 6 items?](http://stackoverflow.com/questions/2851915/rails-each-loop-insert-tag-every-6-items) – Jawa Aug 27 '15 at 08:17
  • not able to integrate in my helper. My row is in my view – Suraj Aug 27 '15 at 08:57

1 Answers1

2

you are asking same question that already asked. please have a look Here

user each_slice to make chunks of 6 of your array then iterate each loop.

e.g

array.each_slice(6) do |chunk|
  chunk.each do |o|
   end 
end 

in your case

rows = ""
part_child_option.each_slice(6) do | six_o |
  row = "<div class='row'>"
  six_o.each do | o |
     row += "your logic"
  end 
  row  += "</div>"
  rows += row
end 
Community
  • 1
  • 1
Asad Ali
  • 640
  • 6
  • 18
  • how can I integrate It in my above helper, – Suraj Aug 27 '15 at 08:45
  • I updated my answer according to your problem. now it can help you I suppose. – Asad Ali Aug 27 '15 at 09:09
  • I tried almost similar answer, It didnt work. See my row is in my view. I have `col-sm-2` which I am looping 6 times, the seventh time It comes I want to go to next line. In the above code It seems you are looping the whole row – Suraj Aug 27 '15 at 09:12
  • The optimal solution that is possible is create rows in helper and feed in html view. remove row div from view and place this method there – Asad Ali Aug 27 '15 at 10:21
  • yes, I am trying that only.. I created another helper, names tic_tac, copied everything from previous method and added row there and removed row from view. But getting wrong number of arguments error http://pastebin.com/Bb5XiLy7 Here's the helper, can you see anything missing? – Suraj Aug 27 '15 at 10:31
  • sorry brother this site is blocked in our country you can past code in comment. – Asad Ali Aug 27 '15 at 10:34
  • u missed a inner loop which will actually create 6 col-sm-2. I updated it a little [HERE](https://jsfiddle.net/asadalibhatti/wayw1yL6/1/) – Asad Ali Aug 27 '15 at 11:40
  • Damn, yes. I was actually using thay but outside of my row and hence each row was getting one image. Thanks Asad. – Suraj Aug 27 '15 at 11:46