2011-09-19 26 views
0

我正在關注PaperClip的railscast並安裝了ImageMagick。 我跟着他的視頻和代碼。上傳的圖像在一個視圖中完美顯示,但在另一個視圖中丟失。我基本上試圖讓上傳的相同圖像顯示在兩個頁面而不是一個頁面上。回形針將圖像加載到一個頁面上,但不顯示在另一頁上。 「Missing.png」

enter image description here

我碰到下面當我檢查對鉻的圖像元素:

<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 %> 

這裏是我的 「微柱」 的模式,因爲我有一直沿着鐵軌教程

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之前的@ instance變量。這不是下面的例子,因爲我得到了一個N​​o Method Error:當我把它變成一個實例變量時,nil:NilClass的undefined方法`photo'。取出後,我能夠得到顯示圖像。**

 <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實例變量,否則我得到的頁面#家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 

任何人都有一個線索,有什麼不對?

+0

您的@microcode變量來自哪裏?你在哪裏設置它? (請閱讀代碼片段) –

+0

我想我們需要看看你的控制器代碼。 – nickgrim

+0

@nickgrim gotcha,抱歉。 – Tony

回答

2

您需要在/photos/medium/missing.png 如果你想要一個顯示

+0

所以你基本上告訴我要將我的圖像重命名爲「missing.png」?我試過沒有運氣。此外,我認爲這會導致我在另一頁上的圖像變爲空白。 – Tony

0

這對我的作品設定丟失的圖像:(user.rb)

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

只需將圖片在您的資產/ images文件夾命名爲:missing_large.png,missing_medium.png,missing_small.png和missing_thumb.png

相關問題