我如何下载远程图像(http协议,url在image_remote_url属性中)并通过Paperclip将其保存为S3的附件?
class Product < ActiveRecord::Base
  require 'open-uri'
  attr_accessor :image_remote_url
  has_attached_file :photo,
    :storage => :s3,
    :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
    :path => ":class/:id/:style.:extension",
    :bucket => "my_bucket",
    :styles => {
      :icon => "32x32#",
  }
  def fetch_image
    # how should this method look ?
  end
end
Run Code Online (Sandbox Code Playgroud)
"fetch_image"方法应该怎么样?
我的网络应用中的用户可以上传文件.我使用Paperclip来处理文件附件问题.如果我想以编程方式删除任何特定的用户上传文件,有什么方法吗?
我正在尝试沿着Paperclip使用ImageMagick来处理我的rails应用程序上的图像.问题是每当我尝试上传图像时,我在终端中得到以下内容:
[paperclip] An error was received while processing: #<Paperclip::NotIdentifiedByImageMagickError: /var/folders/go/goZ833AaFaqyvv5RnLqQmE+++TM/-Tmp-/stream20110107-6356-1xfs9j1-0.jpg is not recognized by the 'identify' command.>
我已将以下内容添加到我的environment/development.rb文件中:
Paperclip.options[:command_path] = "/usr/local/bin"
Run Code Online (Sandbox Code Playgroud)
如果我尝试通过使用"转换"或类似的东西在终端中与w/ImageMagick进行交互,我得到:
dyld: Library not loaded: /opt/local/lib/libltdl.7.dylib
  Referenced from: /usr/local/bin/convert
  Reason: Incompatible library version: convert requires version 10.0.0 or later, but libltdl.7.dylib provides version 9.0.0
Trace/BPT trap
Run Code Online (Sandbox Code Playgroud)
我已经尝试用端口更新所有内容但问题仍然存在.有没有人有任何想法或建议?
terminal imagemagick paperclip osx-snow-leopard ruby-on-rails-3
我刚刚安装了Paperclip并尝试将图标附加到我的模型上.
has_attached_file :icon, 
                  :styles => { :normal => "100x100>", :format => 'png' },
                  :storage => :s3, 
                  :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
                  :url => "/icon/:slug.:extension"
                  :path => "icon/:slug.:extension"
Run Code Online (Sandbox Code Playgroud)
s3.yml包含我的桶名和两个键.
slug interpolation在config/initializers/paperclip.rb中定义为
Paperclip.interpolates('slug') do |attachment, style|
    attachment.instance.cached_slug
end
Run Code Online (Sandbox Code Playgroud)
当我调用game.icon.url时,我收到此错误:
undefined method `icon_file_name' for #<Game:0x4000f50>
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我在Windows 7 x64上运行rails 3.0.4和ruby 1.9.2,如果它有任何区别的话.
我正在使用Paperclip来处理我的Rails应用程序的图像上传,当我使用系统存储时它工作得很好:多个缩略图大小(Paperclip用语中的"样式")保存到文件中,我可以通过传递它来访问它们中的任何一个样式名称为url方法.
但是,当我将应用程序设置为在S3上存储图像时(使用aws-s3 gem),我的S3存储桶中只存储了一个图像.对于它的价值,只存储列出的最后一种样式.所以,如果在我的模型中,我有:
  has_attached_file :photo,
                :styles         => { :large  => "1000x1000>", 
                                     :medium => "600x600>", 
                                     :thumb  => "200x200>" },
                :storage        => :s3,
                :s3_credentials => "#{Rails.root}/config/s3.yml",
                :bucket         => AppConstants.bucket,
                :path           => "pictures/:id/:filename"
Run Code Online (Sandbox Code Playgroud)
只有"拇指"大小才会保存到S3.
有没有人遇到过类似的问题?我怎样才能解决这个问题?
我正在使用Ruby on Rails实现通用媒体库.我选择ActiveAdmin来处理我的任务的管理部分,到目前为止它运行良好,除了一件事:它没有按预期显示"选择文件"对话框.
这是ActiveAdmin的"媒体"部分的表单.我有一个名为"Medium"的模型,其中包含以下字段(除了id和timestamp:
asset_file_nameasset_file_sizeasset_content_typeasset_updated_at我的Medium模型看起来像这样:
class Medium < ActiveRecord::Base
  has_and_belongs_to_many :galleries
  has_and_belongs_to_many :entities
  has_attached_file :asset, :styles => { :medium => "300x300>", :thumb => "100x100>" }
  attr_accessible :asset
end
Run Code Online (Sandbox Code Playgroud)
我将它添加到ActiveAdmin表单中,如下所示:
  form :html => { :enctype => "multipart/form-data" } do |f|  
    f.input :asset, :as => :file
    f.buttons
  end
Run Code Online (Sandbox Code Playgroud)
这是我的ActiveAdmin页面的屏幕截图:

我认为我是如何实现这一点没有错.我已经读过,Formtastic历来存在回形针的问题,我不反对切换到attachment_fu或任何其他合适的解决方案.
我还应该注意:我知道我可以添加自定义部分.这不是我理想的解决方案,因为我想保留Formtastic DSL中的所有内容.
谢谢!
我的模型的cropping方法在循环中调用,在我更新控制器中用户的属性后,它永远不会结束.
User controller code- 
   def change_img  
      @user = current_user
      #this triggers the model's after_update callback
      @user.update_attributes(params[:user])  
      flash[:notice] = "Successfully updated Image."  
      render :action => 'crop'  
   end  
Run Code Online (Sandbox Code Playgroud)
User Model code-  
after_update :reprocess_avatar, :if => :cropping? 
  def cropping?  
   #this method is called infinitely why?
  !crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank? 
  end  
Run Code Online (Sandbox Code Playgroud)
一旦设置了crop_x,crop_y,crop_w和crop_h,该cropping方法将始终返回true,这将继续调用该reprocess_avatar方法.这可能是因为reprocess_avatar方法也在更新avatar用户表的属性.因此再次after_update触发导致循环.
更新后有没有办法只调用一次方法?
没有WebMock,此代码可以正常工作.
提出例外:
Paperclip::AdapterRegistry::NoHandlerError:
   No handler found for #<URI::HTTP:0x007ff3852cefb8 URL:http://www.example.com/images/foo.jpg>
# ./spec/support/api_mock.rb:34:in `process_image_for'
Run Code Online (Sandbox Code Playgroud)
测试:
let( :image_url  ) { 'http://www.example.com/images/foo.jpg'  }
...
stub_request(:post, image_url)
  .to_return(:status => 200, :body => File.read('spec/fixtures/image.jpg'), :headers => {})
...hit Sinatra app...
Run Code Online (Sandbox Code Playgroud)
api_mock.rb:
def self.process_image_for suggestion, params
  if params[:image]
    suggestion.image = URI.parse( params[:image] )  # line 34
    suggestion.save!
  end
end
Run Code Online (Sandbox Code Playgroud) 我为一个练习"插板"类型的应用程序创建了一个:图像白名单的参数.通常,pin.rb会调用
attr_accessible :image
Run Code Online (Sandbox Code Playgroud)
但因为它是Rails 4,我需要使用强参数.因此,我将它们放在pins_controller.rb中:
def pin_params
  params.require(:pin).permit(:description, :image)
end
Run Code Online (Sandbox Code Playgroud)
但是当我尝试上传图像(使用回形针)时,我收到此错误:
ArgumentError in PinsController#create
wrong number of arguments (2 for 1)
Extracted source (around line #29):
27
28
29
30
31
32
  # POST /pins.json
  def create
    @pin = current_user.pins.new(pin_params)
    respond_to do |format|
      if @pin.save
Rails.root: /Users/michaeljdionne/Projects/Rails/omrails
Application Trace | Framework Trace | Full Trace
app/controllers/pins_controller.rb:29:in `create'
Request
Parameters:
{"utf8"=>"?",
 "authenticity_token"=>"awnx8EwrnA/ZoFIBe+ClL6wPbJ+sYnmTvwBCmzNmCHM=",
 "pin"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007fa4ab3796a8 @tempfile=#<File:/var/folders/39/00kqhhp14v9ddj0hrvg9_9bm0000gn/T/RackMultipart20130716-43150-1cflb9r>,
 @original_filename="IMG_0401.jpg",
 @content_type="image/jpeg",
 @headers="Content-Disposition: form-data; name=\"pin[image]\"; filename=\"IMG_0401.jpg\"\r\nContent-Type: image/jpeg\r\n">,
 "description"=>"asd"},
 "commit"=>"Create Pin"}
Run Code Online (Sandbox Code Playgroud)
我知道它与我的参数有关,但我是新手,无法让它工作.
我在网上看了很多.我认为Paperclip可能是我最大的希望.但我不知道如何使用它将图像文件从Rails 4上传到MySQL数据库.是的我知道将它加载到文件夹中更好,但我需要将其加载到数据库中.所以,如果你能帮助我,我将不胜感激.
谢谢
paperclip ×10
amazon-s3 ×3
ruby ×3
activeadmin ×1
attachment ×1
auto-update ×1
crop ×1
file ×1
formtastic ×1
image ×1
imagemagick ×1
mysql ×1
public ×1
rspec ×1
sinatra ×1
terminal ×1
upload ×1
webmock ×1