本文介绍了Rails 回形针 URL 上传问题&如何使它干燥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create URL upload with paperclip.

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

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

The problem is that nothing gets uploaded when I use the image_url fields. I know my code isnt very dry therefor it would be nice if someone had, some tips to rewrite the code.

我有 2 张附加图片,因此有 2 个图片网址.

I have 2 attached images and therefor 2 image URLs.

我的 konkurrancers 表:

My konkurrancers table:

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 模型:

My konkurrancer model:

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

我的控制器创建动作:

  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 %>

推荐答案

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

In the latest version of paperclip (pull request has been merged but i'm not sure about the release) paperclip > 3.1.3 (maybe 3.2 is upcoming; maybe 3.1.4) this is become even easier.

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

以上应该注意下载/临时文件内容/文件名和文件内容类型.

The above should take care of download/tempfile stuff/filename and filecontent type.

享受吧!:)

这篇关于Rails 回形针 URL 上传问题&amp;如何使它干燥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 01:59