一旦上传,Rails回形针就会上传图像路径

时间:2013-12-18 17:12:01

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 paperclip

这是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

任何人都可以帮助我。

1 个答案:

答案 0 :(得分:2)

在您的控制器中

path = @upload.name.url

您只需致电url附件

即可

对于拇指网址,只需添加(:thumb)

即可
path = @upload.name.url(:thumb)