2013-12-18 45 views
1

這裏是upload.rb(模型)圖像路徑Rails的回形針得到上傳上傳一次

class Upload < ActiveRecord::Base 
belongs_to :post 
has_attached_file :name, :styles => { :medium => ["600x400>",:jpg], :thumb => ["122x122>",:jpg]}, url: "/post_images/post_:post_id/:style/:filename" 

def self.image_url 
    "/post_images/post_:post_id/medium/:filename" 
end 

end 

我怎樣才能上傳文件的URL。

我的控制器:

def img_upload 
    @image = Upload.new post_id: post_id, name: params[:Filedata] 
     if @image.save 
      render json: Upload.image_url 
     end 
end 

任何一個可以幫助我。

+0

您認爲或控制器? –

+0

@Justin我也收錄了我的控制器 – kndwsu

回答

2

在你的控制器

path = @upload.name.url 

你只需要調用url到附件

拇指網址,只需添加(:thumb)

path = @upload.name.url(:thumb) 
+0

它顯示的路徑像這樣'/ home/usrname/Documents/site/site_name/public/post_images/post_750/original/Screenshot_from_2013-11-02_00_46_49.png'它在我的工作方式域是否會顯示上面的絕對路徑 – kndwsu

+0

我編輯了我的答案 –

+0

雅工作正常,它可能會得到拇指網址。它顯示原始網址 – kndwsu