2011-05-26 83 views
2

我想創建一些圖片url上傳。我的文件上傳字段有效,但是當我提交一個URL時,它會提交併呈現我的indeks頁面,並且不會上傳圖像。Rails回形針圖片URL上傳不起作用

這裏是我的模型:

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" 

     attr_accessor :photo_url, :photo_url2 

     def photo_url=(url) 
     return if url.blank? 
     self.photo = RemoteUpload.new(url) 
     end 

     def photo_url2=(url) 
     return if url.blank? 
     self.photo2 = RemoteUpload.new(url) 
     end 
end 

我的控制器:

def new 
     @konkurrancer = Konkurrancer.new 
     if !params[:konkurrancer].nil? && !params[:konkurrancer][:photo_url].blank? 
     # user entered a photo url, use the url 
     @konkurrancer.photo_remote_url = params[:konkurrancer][:photo_remote_url] 
     elsif !params[:konkurrancer].nil? && !params[:konkurrancer][:photo] 
     # use file upload 
     @konkurrancer.photo = params[:konkurrancer][:photo] 
    end 

    if !params[:konkurrancer].nil? && !params[:konkurrancer][:photo_url_2].blank? 
     # user entered a photo url, use the url 
     @konkurrancer.photo_remote_url = params[:konkurrancer][:photo_remote_url_2] 
    elsif !params[:konkurrancer].nil? && !params[:konkurrancer][:photo] 
     # use file upload 
     @konkurrancer.photo2 = params[:konkurrancer][:photo2] 
    end 

     respond_to do |format| 
      format.html # new.html.erb 
      format.xml { render :xml => @konkurrancer } 
     end 
     end 

    # POST /konkurrancers 
     # POST /konkurrancers.xml 
     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 

我remote_upload.rb:

require 'open-uri' 

# Make it always write to tempfiles, never StringIO 
OpenURI::Buffer.module_eval { 
    remove_const :StringMax 
    const_set :StringMax, 0 
} 

class RemoteUpload 

    attr_reader :original_filename, :attachment_data 

    def initialize(url) 
    # read remote data 
    @attachment_data = open(url) 

    # determine filename 
    path = self.attachment_data.base_uri.path 

    # we need this attribute for compatibility to paperclip etc. 
    @original_filename = File.basename(path).downcase 
    end 

    # redirect method calls to uploaded file (like size etc.) 
    def method_missing(symbol, *args) 
    if self.attachment_data.respond_to? symbol 
     self.attachment_data.send symbol, *args 
    else 
     super 
    end 
    end 

end 

我的觀點:

<%= simple_form_for [:admin, @konkurrancer], :html => { :multipart => true } do |f| %> 
    <%= f.input :name, :label => 'Titel', :style => 'width:500;' %> 
    <%= f.label :upload_125x125 %> 
    <%= f.file_field :photo, :label => '125x125', :style => 'width:250;' %> 
    <%= f.input :photo_url, :label => 'URL 125x125', :style => 'width:250;' %> 
    <%= f.label :upload_460x60 %> 
    <%= f.file_field :photo2, :label => '460x58', :style => 'width:250;' %> 
    <%= f.input :photo_url2, :label => 'URL 460x58', :style => 'width:250;' %> 
    <%= f.button :submit, :value => 'Create konkurrence' %> 
<% end %> 
+1

你有沒有明確指出它應該出錯的地方?從快速掃描代碼中可以看出,URL是「/public/images/billeder/photo/:id/:basename.:extension」,可能是不正確的。你不應該放棄'/ public'部分嗎? – Kris 2011-05-26 07:23:14

+0

該路線與文件字段上傳正常工作。我不認爲這是它是錯誤的回形針圖像路線。我認爲這與遠程上傳類別有關 – 2011-05-26 08:59:09

回答

4

我正在寫一個漂亮的迴應,然後我發現有人已經寫了相同的東西!

在這裏你去:http://trevorturk.com/2008/12/11/easy-upload-via-url-with-paperclip/

這應該是你在尋找什麼。如果你跌倒了,讓我知道。

祝你好運

+0

我試過這個解決方案。這將是很好,如果你可以回答這個問題:http://stackoverflow.com/questions/6180808/rails-paperclip-url-upload-problem-how-to-make-it-dry – 2011-05-31 13:39:10

2

好像你正試圖訪問一個不符合您有什麼形式的參數:

@konkurrancer.photo_remote_url = params[:konkurrancer][:photo_remote_url] 


<%= f.input :photo_url, :label => 'URL 125x125', :style => 'width:250;' %> 

,你應該使用:PHOTO_URL代替:photo_remote_url控制器?

+0

仍然無效 – 2011-06-03 09:31:35

+0

如果不能運行代碼就很難調試,對不起! – 2011-06-03 17:02:58

+0

@Rails beginner再看看它,你能否在控制器中有新的和創建的方法混淆?我認爲創建是在表單提交後獲取信息的,因此設置遠程上傳的代碼應該可以創建,而不是新建。 – 2011-06-03 17:04:09