John Smith
John Smith

Reputation: 6259

Times loop does not work

I wrote a simple helper for my rails application:

def calendar_build
    5.times do 
      whole_cal
    end
  end

  def whole_cal
      content_tag(:div, :class => "row") do
        small_cal + big_cal
      end
  end

  def small_cal
    content_tag(:div, :class => "col-xs-2 token-text") do
      concat(content_tag(:p, "15.20"))
    end
  end
  def big_cal
    content_tag(:div, :class => "col-xs-10 weite_cal") do
      concat(content_tag(:input,"", class: "form-control input-sm pa"))
    end
  end

How you can see i try to generate 5 whole_cal:

    def calendar_build
        5.times do 
          whole_cal
        end
      end

But in my view this only displays 5 why? Thanks

Upvotes: 0

Views: 59

Answers (3)

kddeisz
kddeisz

Reputation: 5192

Couldn't you do whole_cal*5? String multiplication works in general.

Upvotes: 0

Danny
Danny

Reputation: 6025

You have to add the generated html code to a variable like result, then put result as last line in your method to return the concatenated code

something like:

result = ""

5.times do
  result += html code
end

result

and depending on your html code, you should replace the last line with result.html_safe

Upvotes: 0

usha
usha

Reputation: 29369

The return value of the loop is 5.

1.9.3p194 :056 > 5.times {}
 => 5 

You should probably do

  def calendar_build
   whole_cals = []
    5.times do 
      whole_cals << whole_cal
    end
   whole_cals
  end

And iterate the array in your view

Upvotes: 1

Related Questions