簡體   English   中英

如何從曲別針導軌中的url保存圖像

[英]how to save image from url in paperclip rails

如何從曲別針導軌中的url保存圖像?

而且我有模特文章:

require "open-uri"
class Article < ActiveRecord::Base
attr_accessible :feed_id, :url, :name, :author, :image, :image_caption, :text
has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"

def picture_from_url(url)
  self.image = URI.parse(url)
  self.save
end

def self.add_articles       
      article = create!(
        :feed_id => 1,
        :name => "test",
        :author => "udin",
        :image_caption => "test caption",
        :text => "bla..bla..bla",
      )

 image_url = "http://img.antaranews.com/new/2013/10/big/20131030047.jpg"
      article.picture_from_url(image_url)
    end
  end

如何使圖像的URL保存到我的公共文件夾?

使用gem minimagick解決我的問題

file = MiniMagick::Image.open(image_url)
save_to_local = file.write  "public/assets/file_name.jpg"

做這個

def picture_from_url(url)
  self.update_attributes(:image => File.open(url))
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM