Rails Helper呈现数组,而不是html

时间:2016-02-02 02:12:58

标签: ruby ruby-on-rails-4 helpers partials content-for

如果我致电render_slider_items(["a.png", "b.png", "c.png"])我的网页会显示数组["a.png", "b.png", "c.png"],而不是html。

module ApplicationHelper
 def render_slider_items(filenames)
    filenames.each do |filename|
      content_tag(:div, class: "col-md-3") do 
        tag("img", src: "assets/#{filename}")
      end
    end
  end
end

导致这种情况的原因是什么?

更新 - 解决方案 -

      def render_slider_items(filenames)
        filenames.collect do |filename|
          content_tag(:div, class: "col-md-3") do 
            tag("img", src: "assets/#{filename}")
          end
        end
      end.join().html_safe

1 个答案:

答案 0 :(得分:3)

我猜你这样称呼

#some_file.html.erb
<%= render_slider_items(["a.png", "b.png", "c.png"]) %>

如果是这种情况,发生这种情况的原因是因为.each方法returns the array正在迭代。你最好做这样的事情:

module ApplicationHelper
 def render_slider_items(filenames)
    filenames.collect do |filename|
      content_tag(:div, class: "col-md-3") do 
        tag("img", src: "assets/#{filename}")
      end
    end
  end.join.html_safe
end