这是upload.rb(模型)
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
如何获取上传文件的网址。
我的控制器:
def img_upload
@image = Upload.new post_id: post_id, name: params[:Filedata]
if @image.save
render json: Upload.image_url
end
end
任何人都可以帮助我。
答案 0 :(得分:2)
在您的控制器中
path = @upload.name.url
您只需致电url
附件
对于拇指网址,只需添加(:thumb)
path = @upload.name.url(:thumb)