【问题标题】:Rails paperclip image data being appended after loop循环后附加Rails回形针图像数据
【发布时间】:2012-05-12 04:14:30
【问题描述】:

我正在尝试制作一个显示所有由回形针上传的图片的视图。我设置了回形针,到目前为止,图像已经很好地上传了。但是在创建视图时,由于某种原因,图片的对象数据会附加到所有图像之后。我的模型设置为图片包含图像,即上传的图像文件本身。

在我的控制器中:

class HomeController < ApplicationController
   def index
      @pictures = Picture.all
   end
end

在我看来:

<h2>Pictures</h2>

<%= @pictures.each do |picture| %>
    <div>
        <%= image_tag picture.image.url(:thumb) %>
    </div>
<% end %> 

这是我在所有图片之后在页面上看到的内容。

[#<Picture id: 6, created_at: "2012-05-11 18:57:21", updated_at: "2012-05-11 18:57:21", image_file_name: "puppy1.jpg", image_content_type: "image/jpeg", image_file_size: 150222, image_updated_at: "2012-05-11 18:57:21", title: "Doggy", caption: "">, #<Picture id: 7, created_at: "2012-05-11 19:28:56", updated_at: "2012-05-11 19:28:56", image_file_name: "puppy1.jpg", image_content_type: "image/jpeg", image_file_size: 150222, image_updated_at: "2012-05-11 19:28:56", title: "Doggy number 2", caption: "">]

有趣的是,即使我删除了循环中的所有代码,对象数据仍然会出现,但如果我单独加载每张图片,它就可以正常工作。

@picture = Picture.find(6)

<%= image_tag @picture.image.url(:thumb) %>

【问题讨论】:

    标签: ruby-on-rails ruby paperclip


    【解决方案1】:

    替换

    <%= @pictures.each do |picture| %>
    

    <% @pictures.each do |picture| %>
    

    '=' 也返回 @pictures 数组

    【讨论】:

    • 每个人都有过这样的感觉,而且还会再次发生 :)
    猜你喜欢
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-15
    相关资源
    最近更新 更多