Ruby on rails rails将代码从视图提取到帮助器(内容\标记错误?)

Ruby on rails rails将代码从视图提取到帮助器(内容\标记错误?),ruby-on-rails,ruby-on-rails-3,content-tag,Ruby On Rails,Ruby On Rails 3,Content Tag,在我看来,我得到了以下代码: <ul class="thumbnails"> <% Photo.find_all_by_id(my_params, limit: 10).each do |p| %> <li class="thumbnail"> <%= image_tag(p.url(:fb)) %> </li> <% end %> </ul> 但当我在视图中使用时,它仅呈现

在我看来,我得到了以下代码:

<ul class="thumbnails">
  <% Photo.find_all_by_id(my_params, limit: 10).each do |p| %>
    <li class="thumbnail">
      <%= image_tag(p.url(:fb)) %>
    </li>
  <% end %>
</ul>
但当我在视图中使用
时,它仅呈现:

<ul class="thumbnails"></ul>

    我做错了什么?

    看到您的代码后发现,在调用helper方法时,您没有传递
    photo\u id
    参数

    <%= photos_thumbs_by_ids(photo_ids) %>
    
    
    

    由于没有用于查找照片的ID,因此它将返回零个项目,因此不会创建任何
  • 项目。

    我在看到您的代码后发现,在调用帮助器方法时,您没有传递
    照片ID
    参数

    <%= photos_thumbs_by_ids(photo_ids) %>
    
    
    
    由于没有用于查找照片的ID,因此它将返回零个项目,因此不会创建任何
  • 项目

    <%= photos_thumbs_by_ids(photo_ids) %>
    
    def photos_thumbs_by_ids(photos_ids)
        content_tag :ul, :class => "thumbnails" do
          Photo.find_all_by_id(photos_ids, limit: 10).each do |p|
            content_tag :li, :class => "thumbnail" do
              concat(image_tag(p.url(:fb)))
            end
          end
        end
      end
    
    
    def照片、拇指和ID(照片和ID)
    内容标签:ul,:class=>“缩略图”do
    照片。按id查找所有(照片id,限制:10)。每个都有|
    content_标签:li,:class=>“缩略图”do
    concat(图像标签(p.url(:fb)))
    结束
    结束
    结束
    结束
    
    尝试以下方法

    <%= photos_thumbs_by_ids(photo_ids) %>
    
    def photos_thumbs_by_ids(photos_ids)
        content_tag :ul, :class => "thumbnails" do
          Photo.find_all_by_id(photos_ids, limit: 10).each do |p|
            content_tag :li, :class => "thumbnail" do
              concat(image_tag(p.url(:fb)))
            end
          end
        end
      end
    
    
    def照片、拇指和ID(照片和ID)
    内容标签:ul,:class=>“缩略图”do
    照片。按id查找所有(照片id,限制:10)。每个都有|
    content_标签:li,:class=>“缩略图”do
    concat(图像标签(p.url(:fb)))
    结束
    结束
    结束
    结束
    
    问题在于:

    它返回一个数组,而不是字符串。尝试用“地图”收集它并进行连接

    因为至少渲染是通过有用的方法完成的。仅当结果为字符串时,此方法才会放入输出缓冲区,并且不要尝试使用to_s。

    问题在于:

    它返回一个数组,而不是字符串。尝试用“地图”收集它并进行连接


    因为至少渲染是通过有用的方法完成的。仅当结果是字符串时,此方法才会放入输出缓冲区,并且不要尝试使用to_s.

    对不起,只是没有在示例中错误地添加参数-我确实将其发送给帮助者或者,只是没有在示例中错误地添加参数-我确实将其发送给帮助者更新:对不起,只是没有在示例中错误地添加参数-我确实将其发送给helperupdate:抱歉,只是没有在示例中错误地添加参数-我确实将其发送给helperOMG!是的,对不起,我忘了html_safe.OMG!是的,对不起,我忘了带保险箱。
    Photo.find_all_by_id(photos_ids, limit: 10).each do |p|
    ...
    end
    
    module PhotosHelper
      def photos_thumbs_by_ids(photos_ids)
        content_tag :ul, :class => "thumbnails" do
          Photo.find_all_by_id(photos_ids, limit: 10).map { |p|
            content_tag :li, :class => "thumbnail" do
               image_tag(p.url(:fb))
            end
          }.join.html_safe
        end
      end
    end