【问题标题】:Paperclip loads image on one page, but doesn't display on another. "Missing.png"Paperclip 在一个页面上加载图像,但不显示在另一个页面上。 “失踪.png”
【发布时间】:2014-03-09 10:00:43
【问题描述】:

我正在关注 PaperClip 的 railscast 并安装了 ImageMagick。 我也跟着他的视频和代码。上传的图像在一个视图中完美显示,但在另一个视图中丢失。我实际上是在尝试上传相同的图像以显示在两个页面上,而不仅仅是一个页面。

当我检查 chrome 上的图像元素时,我得到以下信息:

<img alt="Missing" src="/photos/medium/missing.png?1316405973">

这是我的表格:

<%= form_for @micropost, :html => { :multipart => true } do |f| %>
  <%= render 'shared/error_messages', :object => f.object %>
  <div class="field">
    <%= f.text_area :content %>
  </div>
  <div class="actions">
    <%= f.submit "Submit" %>
  </div>
  <div class="photo">
  <%= f.file_field :photo %>
  </div>
<% end %>

这是我的“micropost”模型,因为我一直在关注 Rails 教程

    class Micropost < ActiveRecord::Base
      attr_accessible :content, :photo, :photo_file_name, :photo_content_type,  :photo_file_size, :photo_updated_at

      belongs_to :user
      has_attached_file :photo,
            :url  => "/assets/products/:id/:style/:basename.:extension",
            :path => ":rails_root/public/assets/products/:id/:style/:basename.:extension"

    validates_attachment_presence :photo
    validates_attachment_size :photo, :less_than => 5.megabytes
    validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png'
end

这是我的 _micropost.html.erb 文件,第 4 行带有 image_tag。

****请注意,我删除了 micropost 这个词之前的 @ 实例变量。这不遵循示例,因为当我将其设置为实例变量时,我得到了 No Method Error: undefined method `photo' for nil:NilClass。删除后,我可以显示图像。**

        <tr>
      <td class="micropost">
        <span class="content"><%= micropost.content %></span>
        <%= image_tag micropost.photo.url(:medium) %>   #### This is the code added ########
        <span class="timestamp">
   ....
....
...
      </td>
      <% end %>
    </tr>

但是,在提要页面上,我确实使用了@micropost 实例变量,否则我在 Pages#home 中得到 NameError,未定义的局部变量或 Class 的方法 `micropost'。使其成为实例变量至少会在页面上显示一些内容。这是缺少图片的视图。 feed_item.html.erb

   <tr>
      <td class="gravatar">
  ...
  ...
        </span>
        <span class="content"><%= feed_item.content %></span>
            <%= image_tag @micropost.photo.url(:medium) %>    #### inserted code##
        <span class="timestamp">
          Posted <%= time_ago_in_words(feed_item.created_at) %> ago.
  ....
....
      <% end %>
    </tr>

****编辑**

这是我的控制器代码:

class MicropostsController < ApplicationController
 before_filter :authenticate, :only => [:create, :destroy]
  before_filter :authorized_user, :only => :destroy


   def create
    @micropost = current_user.microposts.build(params[:micropost])
    if @micropost.save
      flash[:success] = "Blurb posted!"
      redirect_to root_path 
    else
      @feed_items = []
      render 'pages/home'
    end
  end

有人知道出了什么问题吗?

【问题讨论】:

  • 你的@microcode 变量来自哪里?你在哪里设置? (请摘录代码)
  • 我认为我们需要查看您的控制器代码。
  • @nickgrim gotcha,对不起。
  • @Marcel Jackwerth 刚刚编辑了帖子。谢谢你们的意见。

标签: ruby-on-rails paperclip


【解决方案1】:

你需要在/photos/medium/missing.png设置一个缺失的图片 如果你想显示一个

【讨论】:

  • 所以您实际上是在告诉我将图像重命名为“missing.png”?我试过了,但没有运气。另外,我认为这会导致我在其他页面上的图像变为空白。
【解决方案2】:

这对我有用:(user.rb)

has_attached_file :avatar, :styles => { :small => "52x52",
:medium => "200x200>", :large=> "300x300", :thumb => "100x100>" },
                              :default_url => "missing_:style.png"

只需将图片放在您的 assets/images 文件夹中,命名为:missing_large.png、missing_medium.png、missing_small.png 和 missing_thumb.png

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-24
    • 2020-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-22
    相关资源
    最近更新 更多