ruby-on-rails - Rails 回形针 URL 上传问题及如何使其 DRY

标签 ruby-on-rails ruby ruby-on-rails-3

我正在尝试使用回形针创建 URL 上传。

我已遵循此指南:http://trevorturk.com/2008/12/11/easy-upload-via-url-with-paperclip/

问题是当我使用 image_url 字段时没有上传任何内容。我知道我的代码不是很干,因此如果有人有一些重写代码的技巧,那就太好了。

我有 2 个附加图像,因此有 2 个图像 URL。

我的 konkurrancers 表:

photo_file_name        varchar(255) 
photo_content_type      varchar(255) 
photo_file_size         int(11)
photo_updated_at        datetime    
photo2_file_name        varchar(255)
photo2_content_type     varchar(255) 
photo2_file_size        int(11)
photo2_updated_at       datetime
image_remote_url        varchar(255)
image_remote_url_2      varchar(255)

我的 konkurrancer 模型:

class Konkurrancer < ActiveRecord::Base
has_attached_file :photo,
                  :url  => "/public/images/billeder/photo/:id/:basename.:extension",
                  :path => ":rails_root/public/images/billeder/photo/:id/:basename.:extension"
has_attached_file :photo2,
                  :url  => "/public/images/billeder/photo2/:id/:basename.:extension",
                  :path => ":rails_root/public/images/billeder/photo2/:id/:basename.:extension"

 before_validation :download_remote_image, :if => :image_url_provided?
  before_validation :download_remote_image_2, :if => :image_url_2_provided?
  validates_presence_of :image_remote_url, :if => :image_url_provided?, :message => 'is invalid or inaccessible'
  validates_presence_of :image_remote_url_2, :if => :image_url_2_provided?, :message => 'is invalid or inaccessible'

private

  def image_url_provided?
    !self.image_url.blank?
  end

  def image_url_2_provided?
    !self.image_url_2.blank?
  end

  def download_remote_image
    self.photo = do_download_remote_image
    self.image_remote_url = image_url
  end

    def download_remote_image_2
    self.photo2 = do_download_remote_image_2
    self.image_remote_url_2 = image_url_2
  end

  def do_download_remote_image
    io = open(URI.parse(image_url))
    def io.original_filename; base_uri.path.split('/').last; end
    io.original_filename.blank? ? nil : io
  rescue # catch url errors with validations instead of exceptions (Errno::ENOENT, OpenURI::HTTPError, etc...)
  end
  def do_download_remote_image_2
    io = open(URI.parse(image_url_2))
    def io.original_filename; base_uri.path.split('/').last; end
    io.original_filename.blank? ? nil : io
  rescue # catch url errors with validations instead of exceptions (Errno::ENOENT, OpenURI::HTTPError, etc...)
  end
end

我的 Controller 创建操作:

  def create
    @konkurrancer = Konkurrancer.new(params[:konkurrancer])

    respond_to do |format|
      if @konkurrancer.save
        format.html { redirect_to(:admin_konkurrancers, :notice => 'Konkurrancer was successfully created.') }
        format.xml  { render :xml => :admin_konkurrancers, :status => :created, :location => @konkurrancer }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @konkurrancer.errors, :status => :unprocessable_entity }
      end
    end
  end

我的表格:

<%= simple_form_for [:admin, @konkurrancer], :html => { :multipart => true } do |f| %>
    <%= f.label :upload_125x125 %>
    <%= f.file_field :photo, :label => '125x125', :style => 'width:250;' %>
    <%= f.input :image_url_2, :label => 'URL 125x125', :style => 'width:250;' %>
    <%= f.label :upload_460x60 %>
    <%= f.file_field :photo2, :label => '460x58', :style => 'width:250;' %>
    <%= f.button :submit, :value => 'Create konkurrence' %>
<% end %>

最佳答案

在最新版本的 paperclip 中(拉取请求已合并,但我不确定是否发布)paperclip > 3.1.3(也许 3.2 即将推出;也许 3.1.4)这变得更加容易。

self.photo = URI.parse("http://something.com/blah/image.png")

上面应该处理下载/临时文件内容/文件名和文件内容类型。

享受吧! :)

关于ruby-on-rails - Rails 回形针 URL 上传问题及如何使其 DRY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6180808/

相关文章:

ruby-on-rails - 尝试加载 gem 时出错 'devise. ActiveSupport: Duration can' t 被强制转换为整数

ruby-on-rails - 如何删除 routes.rb 中为路径中的参数生成的前缀

ruby-on-rails - collection_select 中的 Rails 编码 UTF-8 错误

ruby-on-rails-3 - 带有嵌套资源的 Rails best_in_place gem

ruby-on-rails - 异常通知程序插件不发送电子邮件

ruby-on-rails - 干净的解决方案,用于在两次rspec测试之间重置类变量

ruby-on-rails - 如何正确管理开发和生产环境的迁移?

ruby-on-rails - 如何设置我的 Rails 应用程序以通过 Phusion Passenger 运行?

ruby-on-rails-3 - 升级到 osx lion,bundler 得到 native 扩展错误 - 没有这样的文件或目录

ruby-on-rails-3 - 延迟作业 : how to force processing of failed jobs