Ruby on rails 在带有回形针的模型中使用Rails环境URL

Ruby on rails 在带有回形针的模型中使用Rails环境URL,ruby-on-rails,paperclip,Ruby On Rails,Paperclip,在我的用户模型中,我的回形针设置如下: has_attached_file :profile_pic, :styles => {:large => "300x300>", :medium => "150x150>", :small => "50x50#", :thumb => "30x30#" }, :default_style => :thumb,

在我的用户模型中,我的回形针设置如下:

  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

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

在Rails 3中添加:
在模型中包括Rails.application.routes.url\u helpers

在Rails 2中,在模型中添加:
包括ActionController::UrlWriter

然后
root\u url
包含应用程序的基本url。那么你可以做:

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",

根url无法立即工作

在使用#{root_url}之前,需要分配Rails.application.routes.default_url_options[:host]

因此,您可以将配置设置到环境中。用于staging.rb/production.rb/development.rb

  config.after_initialize do
    Rails.application.routes.default_url_options[:host] = 'http://localhost:3000'
  end

最简单的替代方法:

class Post < ActiveRecord::Base
  include Rails.application.routes.url_helpers

    validates :image, presence: true

      has_attached_file :image, styles: { :medium => "640x", thumb: "100x100#" } # # means crop the image
        validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/

  def image_url
   relative_path =  image.url(:medium)
   self.add_host_prefix relative_path
  end

 def thumb_url
   relative_path = image.url(:thumb)
   self.add_host_prefix relative_path
 end

  def add_host_prefix(url)
    URI.join(root_url, url).to_s
  end
end
class Api::ImagesController < ApplicationController

  def index
    @posts =  Post.all.order(id: :desc)
    paginated_records = @posts.paginate(:page => params[:page], :per_page => params[:per_page])
    @posts = with_pagination_info( paginated_records )
    render :json => @posts.to_json({:methods => [:image_url, :thumb_url]})
  end
end
包括在你的课堂上

include Rails.application.routes.url_helpers
以我的模型为例获取回形针图像绝对url:

class Post < ActiveRecord::Base
  include Rails.application.routes.url_helpers

    validates :image, presence: true

      has_attached_file :image, styles: { :medium => "640x", thumb: "100x100#" } # # means crop the image
        validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/

  def image_url
   relative_path =  image.url(:medium)
   self.add_host_prefix relative_path
  end

 def thumb_url
   relative_path = image.url(:thumb)
   self.add_host_prefix relative_path
 end

  def add_host_prefix(url)
    URI.join(root_url, url).to_s
  end
end
class Api::ImagesController < ApplicationController

  def index
    @posts =  Post.all.order(id: :desc)
    paginated_records = @posts.paginate(:page => params[:page], :per_page => params[:per_page])
    @posts = with_pagination_info( paginated_records )
    render :json => @posts.to_json({:methods => [:image_url, :thumb_url]})
  end
end
在:

Your\u project\u root\u deir/config/environments/development.rb


虽然助手只能在视图中访问,但这是一个有效的解决方案。

谢谢,但这不起作用默认图片的URL最终是:{root\u URL}/images/profile\u pics/default\u small.pngI在我的模式中添加了include,并按指定设置默认URL。我需要在某个地方设置根url吗?我在谷歌上找不到。你确定在
:default_url
的字符串周围加了双引号吗?否则将不会发生插值。