2014-10-28 65 views
0

每一个!我正在使用回形针上传文件回形针 - 如何访问保存的文件

class Model 
    include Mongoid::Document 
    include Mongoid::Paperclip 

    has_many :myfiles 
end 

class Myfile 
    include Mongoid::Document 
    include Mongoid::Paperclip 

    belongs_to :model 
    has_mongoid_attached_file :file, 
    :path => ":rails_root/public/uploads/:class/:id/:basename.:extension", 
end 

我的问题是如何访问保存后上传的文件?我想:

@model.myfiles.first.path 
@model.myfiles.first.url 

提示错误:

NoMethodError: undefined method `path' for #<Myfile:0x007fd22b336f80> 

非常感谢您的帮助!

回答

1

试试:)

@model.myfiles.first.file.path 
@model.myfiles.first.file.url 

可以委托这些领域,如果想

class Myfile 
    include Mongoid::Document 
    include Mongoid::Paperclip 

    belongs_to :model 
    has_mongoid_attached_file :file, 
    :path => ":rails_root/public/uploads/:class/:id/:basename.:extension" 

    delegate :url, :path, to: :file, allow_nil: true, prefix: false 
end 

然后你可以使用

@model.myfiles.first.path 
@model.myfiles.first.url 
+0

非常感谢您!有用! – 2014-10-28 16:16:10