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')