2013-05-07 40 views
4

我正在尋找一種方法將照片上載到數據庫並在頁面上顯示照片。我正在嘗試載波。我認爲它正確地上傳了照片,但是當它顯示圖像時,它只會提供它的位置(url)。用Carrierwave不顯示圖像

這裏是上傳

class Image < ActiveRecord::Base 
attr_accessible :image, :title 
    mount_uploader :image, ImageUploader 
end 

這裏被用來上傳文件

<%= form_for @image, :html => {:multipart => true} do |f| %> 
    <% if @image.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@image.errors.count, "error") %> prohibited this image from being saved:</h2> 

     <ul> 
     <% @image.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <div class="field"> 
    <%= f.label :image %><br /> 
    <%= f.file_field :image %> 
    </div> 
    <div class="field"> 
    <%= f.label :title %><br /> 
    <%= f.text_field :title %> 
    </div> 
    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

這是顯示圖像

<p id="notice"><%= notice %></p> 

<p> 
    <b>Image:</b> 
    <%= @image.image_url.to_s %> 
</p> 

<p> 
    <b>Title:</b> 
    <%= @image.title %> 
</p> 


<%= link_to 'Back', images_path %> 

這是打印出來的表格:

圖片:/uploads/image/image/2/946612_524939910875157_1455921715_n.jpg

標題:海報

+0

變化'<%= @ image.image_url.to_s%>''到<%= IMAGE_TAG @ image.image_url.to_s%>' – 2013-05-07 10:36:05

回答

4

顯示此圖像,試試這個。

<%= image_tag @image.image_url.to_s if @image.image_url.present? %> 
+0

你可以直接使用'@ image.image_url',有沒有必要對於'「#{@ image.image_url}」' – 2013-05-07 11:14:02

+0

是的Anezio Campos你是對的。已經更新了我的答案。 :) – rony36 2013-05-07 11:15:31

+0

謝謝兩個:)它的工作。非常感謝 – jordankoal 2013-05-07 13:49:02