2016-12-14 54 views
0

我正在使用Carrierwave,以便用戶可以上傳圖像。我正在創建一個博客網站。我沒有錯誤,但圖像沒有出現。我希望圖像顯示在發佈視圖中。這裏是我的代碼:Ruby-On-Rails如何顯示用戶上傳的圖像

Post.rb

mount_uploader :image, ImageUploader 

帖子_form.html.erb

<div> 
    <%= form_for @post do |f|%> 
    <%= f.label :image %> 
    <%= f.file_field :image %> 
    </div> 

image_uploader.rb

def store_dir 
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 
    end 

    def extension_white_list 
    %w(jpg jpeg gif png) 
    end 

帖子show.html.erb

<strong>Image:</strong> 
    <%= image_tag @post.image_url%> 
</p> 

當我運行我的應用程序並查看帖子時,它只是說Image:下面沒有圖像。

UPDATE:

我安裝的ImageMagick/GraphicsMagick工具,當我去保存它張貼說:

1 error prohibited this post from being saved: 
Image Failed to manipulate with MiniMagick, maybe it is not an image? `Original Error: ImageMagick/GraphicsMagick is not installed` 

,並在其下面顯示的圖像,但它不會保存職位。

+0

try @ post.image_url.to_s –

+0

@TonyVincent不幸的是,沒有喜悅! – Jill

回答

1

雖然form_for設置自動編碼multipart,但在我的項目中,我仍然定義它。試試這個:

<%= form_for(@post, hmtl: { multipart: true }) do |f| %> 
+0

非常感謝:D – Jill

+1

感謝您!我第一個接受的答案! Celebraaation! :D –

+2

耶! :D快樂的日子! – Jill