繁体   English   中英

使用Paperclip远程上传URL

[英]Remote URL uploading with Paperclip

我正在尝试允许用户通过远程URL上传图像。

我正常的选择文件图像上传效果很好。

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

日志:

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。

任何帮助将不胜感激。

解:

在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添加到我的图像表和繁荣中,问题解决了!

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仅在下载图像时出现网络错误时才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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM