2

我回形针路径是:回形针给出错误的网址。怎么修?在模型

has_attached_file :image, :path => ":rails_root/app/assets/images/article_images/:id/:style_:basename.:extension" 

但是,当我打电话article.image.url,这里是网址我得到(在被折断):

/system/images/64294/original/fantastik.jpg?1324288670 

回答

6

您还需要设置URL,你想要的是:

has_attached_file :image, 
        :path => ":rails_root/app/assets/images/article_images/:id/:style_:basename.:extension" 
        :url => "/app/assets/images/article_images/:id/:style_:basename.:extension" 

我会问唯一的问题是,如果你把它们存放在一个好的地方。通常,他们会进去

/public/... 

或者在像s3这样的其他服务上。但是,这是你如何修改网址

希望这有助于!

0

像@andrewpthorp提那是因为你只切换路径,但你可以做到这一点更干

回形针默认:

:url     => "/system/:attachment/:id/:style/:filename", 
:path     => ":rails_root/public:url", 

你可以看到,URL可以是路径的一部分,所以你的配置应该看起来像:

has_attached_file :image, 
    :url => "/app/assets/images/article_images/:id/:style_:basename.:extension", 
    :path => ":rails_root:url" 

要小心,平常的服务器(apache,nginx)只从公共目录提供文件。

更多选项has_attached_file你可以找到here

+0

在我看来是默认值在最近的版本已经改变。使用这里显示的解决了我的Rails应用程序的一个问题。 – halfdan 2012-08-14 14:56:38

0

这是如何与解决问题:DEFAULT_URL:

:default_url => ActionController::Base.helpers.asset_path('empty-event-cover.png')