使用Paperclip上传远程URL

时间:2015-09-17 16:15:09

标签: ruby-on-rails ruby upload paperclip

我正在尝试允许用户通过远程网址上传图片。

我的正常选择文件图片上传效果非常好。

我正在提交关于URL上传图像的回滚事务。

LOG:

Started POST "/images" for 127.0.0.1 at 2015-09-16 23:33:52 -0600
Processing by ImagesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"rYIESJ0zLKaXx1ci+9utsZUxEEBs8rS5eanKvYoz4zoESAgqI8 AaPlyy00SlmStBJMYt4RRiAsv+v4ksuWGdeA==", "image"=>{"image_url"=>"http://bangalorefoodfete.com/wp-content/uploads/2015/09/food-61.jpg"}, "commit"=>"Submit Photo"}
  (0.1ms)  BEGIN
  (0.2ms)  ROLLBACK
  (0.1ms)  BEGIN
  (0.3ms)  ROLLBACK
  Rendered images/new.html.erb within layouts/application (0.9ms)
  Rendered layouts/_head.html.erb (7.3ms)
  Rendered layouts/_navigation_links.html.erb (0.4ms)
  Rendered layouts/_navigation.html.erb (1.3ms)
  Rendered layouts/_messages.html.erb (0.1ms)
  Completed 200 OK in 5398ms (Views: 48.2ms | ActiveRecord: 0.8ms)

编辑:

收到错误讯息。

图片远程网址无效或无法访问

此通知摘自此处:

image.rb:

validates_presence_of :image_remote_url, :if => :image_url_provided?, :message => 'is invalid or inaccessible'

Image.rb:

require 'open-uri'

class Image < ActiveRecord::Base

has_attached_file :image, 
:path => ":rails_root/public/system/:attachment/:id/:style/:filename",

:url => "/system/:attachment/:id/:style/:filename", 
:styles => { :medium => "600x600>", :thumb => "100x100#" }

before_validation :download_remote_image, :if => :image_url_provided?

validates_attachment :image,
content_type: { content_type: ["image/jpeg",       "image/jpg", "image/gif", "image/png"] }


validates_presence_of :image_remote_url, :if => :image_url_provided?, :message => 'is invalid or inaccessible'

private

 def image_url_provided?
 !self.image_url.blank?
end

def download_remote_image
 io = open(URI.parse(image_url))
 self.original_filename = io.base_uri.path.split('/').last
 self.image = io
 self.image_remote_url = image_url
 rescue # catch url errors with validations instead of exceptions    (Errno::ENOENT, OpenURI::HTTPError, etc...)
end
end

相关的new.html.erb:

<%= form.text_field :image_url %><br>

images_controller.rb:

lass ImagesController < ApplicationController
 before_action :find_image, only: [:show]

 def new
   @image = Image.new
 end  


 def create
  @image = Image.create( user_params )


   if @image.save
     redirect_to @image

   else
     render 'new'

   end
  end

  def show

  end  


  private

  def user_params
   params.require(:image).permit(:image, :title, :description, :image_remote_url, :image_url)
  end  

 def find_image

    @image = Image.find(params[:id])

  end
end

相关的schema.rb:

create_table "images", force: :cascade do |t|
   t.string   "title"
   t.text     "description"
   t.datetime "created_at",         null: false
   t.datetime "updated_at",         null: false
   t.string   "image_file_name"
   t.string   "image_content_type"
   t.integer  "image_file_size"
   t.datetime "image_updated_at"
   t.string   "image_url"
   t.string   "image_remote_url"
end

我正在使用Paperclip v4.3运行Rails 4.2.4。

非常感谢任何帮助。

SOLUTION:

在models / image.rb中我改变了:

def download_remote_image
io = open(URI.parse(image_url))
self.original_filename = io.base_uri.path.split('/').last
self.image = io
self.image_remote_url = image_url
 rescue
end
end

到此:

 def download_remote_image
io = open(URI.parse(image_url))
self.original_filename = io.base_uri.path.split('/').last
self.image = io
self.image_remote_url = image_url

end
end

我删除了救援,创建了一个迁移,将original_filename添加到我的图像表并繁荣,问题解决了!

1 个答案:

答案 0 :(得分:1)

ROLLBACK通常是由验证错误引起的。将其放在new.html.erb的顶部以显示验证错误:

<% if @image.errors.any? %>
  <% @image.errors.full_messages.each do |message| %>
     <p> <%= message %> </p>
  <% end %>
<% end %> 

根据您的代码,只有在下载图片时出现网络错误时才能获得验证错误Image remote url is invalid or inaccessible

您可以移除rescue行以进行调试,看看您遇到了什么样的错误。

  

我收到此错误:对于#

,未定义的方法`original_filename ='

此行会导致您的错误:

self.original_filename = io.base_uri.path.split('/').last

如果您不需要存储original_filename,只需删除该行即可。否则,请将此列添加到images表中:

$ rails g migration AddOriginalFilenameToImages original_filename
$ rake db:migrate
相关问题