开发者

content_tag does return blank

I have a problem with content_tag into an helper, this piece of code

  def renderize(place_holder)
    content_tag(:div, "hello") do
      @raw_estimate.field_estimates.where(:field_position_id => FieldPosition.where(:place_holder => place_holder).first).each do |field|
        if field.field_type.alias == "input"
          content_tag :div do
            field.is_validate ? label_value = "*#{field.name}" : label_value = field.name
            content_tag(:label_tag, label_value) +
            text_field_tag("estimate[field_#{field.name.downcase.gsub(/\W+/, '')}]")
          end
        end
      end
    end
  end

does not return anything, wh开发者_运维问答at am I missing? Thank you


don't use each for interacting array, use collect will will return an array of your rendered tags see http://apidock.com/rails/ActionView/Helpers/TagHelper/content_tag for the last few comments


This line is suspicious:

 :field_position_id => FieldPosition.where(:place_holder => place_holder).first

As far as I can understand, the left part is an id, while right part returns object FieldPosition, and not its id. That's why no objects are returned, and your helper has no content.


As mentioned in another answer, using each won't work -- it doesn't return the internal contents of the block. Just as an example, try running this in irb:

result = 1.upto(3) do |x| x; end

result will be "1"

Here's a cleaner test of your code, try it and see -- it won't return anything:

  def random_test
    content_tag(:div, "hello") do
      1.upto(3) do |x|
        content_tag(:p, x)
      end
    end
  end

Instead, you need to collect the output of your iterator, something along these lines:

  def random_test
    content_tag(:div, "hello") do
      1.upto(3) do |x|
        content_tag(:p, x)
      end
    end
  end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜