在带有回形针的模型中使用 Rails 环境 URL [英] Using the Rails Environment URL in a MODEL with paperclip

查看:36
本文介绍了在带有回形针的模型中使用 Rails 环境 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的用户模型中,我有一个像这样的回形针设置:

in my users model I have a paperclip setup like this:

  has_attached_file :profile_pic, 
                    :styles => {:large => "300x300>", :medium => "150x150>", :small => "50x50#", :thumb => "30x30#" },
                    :default_style => :thumb,
                    :default_url => '/images/:attachment/default_:style.png',

如何设置默认 URL,包括完整 URL?

How do I make the default URL, include the full URL?

http://0.0.0.0:3000/images/:attachment/default_:style.png 
or http://sitename.com/images/:attachment/default_:style.png

推荐答案

在 Rails 3 中添加:include Rails.application.routes.url_helpers 在你的模型中.

In Rails 3 add: include Rails.application.routes.url_helpers in your model.

在 Rails 2 中添加:include ActionController::UrlWriter 在你的模型中.

In Rails 2 add: include ActionController::UrlWriter in your model.

然后 root_url 包含您的应用程序的基本网址.那么你可以这样做:

Then root_url contains the base url of your app. So then you can do:

has_attached_file :profile_pic, 
                    :styles => {:large => "300x300>", :medium => "150x150>", :small => "50x50#", :thumb => "30x30#" },
                    :default_style => :thumb,
                    :default_url => "#{root_url}/images/:attachment/default_:style.png",

这篇关于在带有回形针的模型中使用 Rails 环境 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆