2
我想在我的Rails應用程序中上傳圖像。但是,我無法這樣做。我比較新的軌道,我需要一些幫助。使用回形針在導軌中上傳圖像3
我在這裏所示的「圖片」模式 -
class Picture < ActiveRecord::Base
attr_accessible :photo, :tag
has_attached_file :photo, :styles => { :medium => "300x300>",
:large => "1000x1000>", :thumb => "100x100>" }
end
我這裏所示的「照片」控制器 -
class PicturesController < ApplicationController
def new
@picture = Picture.new
end
def create
@picture = Picture.create(params[:picture])
if @picture.save
flash.now[:success] = "Image Uploaded"
redirect_to @picture
else
render 'new'
end
end
def index
end
def show
@picture = Picture.find(params[:id])
send_data @picture.photo, :type => 'image/png', :disposition => 'inline'
end
def imageshow
end
end
最後,這些都是我的「新」和「秀」觀點:
「new.html.erb」
<h1>Upload an Image</h1>
<div>
<%= form_for(@picture, :html => {:multipart => true}) do |f| %>
<%= f.file_field :photo %>
<%= f.label :tag %>
<%= f.text_field :tag %>
<%= f.submit "Submit", class: "btn btn-large btn-primary" %>
<% end %>
</div>
「show.html.erb」
<h1>Displaying Uploaded Image</h1>
<div>
<%= image_tag @picture.photo.url %>
<%= image_tag @picture.photo.url(:large) %>
</div>
我能夠達到上傳頁面(即圖片控制器new.html.erb)。然而,當我上載圖片,我得到以下錯誤:
http://10.102.119.20:3000/pictures/12「這個形象‘’無法顯示,因爲它包含錯誤」
我的查詢是:
- 圖像上傳到服務器上的哪裏?
- 是否有任何配置需要完成?
- 我的代碼還有其他問題嗎?
在此先感謝您的任何幫助..!
我知道,就在上次我使用回形針時,圖像被上傳到'public'目錄 – iGbanam
Yasky,那是真的。我不知何故發現,默認情況下,圖像上傳到/公共目錄。確切地說,/ root/rails_project/sample_app/public/system/pictures/photos ..但是,我無法在圖片控制器中使用show方法顯示圖像。你可以看到我在上面的show方法中使用了什麼。有任何想法嗎?? –