2

I am trying to create some image url upload. My file upload field works, but when I submit a URL it submit and renders my indeks page and no image gets uploaded.

Here is my model:

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

My controller:

    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

My 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

My view:

<%= 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 %>
2
  • 1
    Have you pinpointed where it supposedly go wrong? What I can see from quickly scanning your code, is that the url "/public/images/billeder/photo/:id/:basename.:extension" probably is not correct. Shouldn't you drop the /public part? Commented May 26, 2011 at 7:23
  • The route works fine with the file field upload. I dont think it is the paperclip image route that it is wrong with. I do think it have something to do with the remote upload class Commented May 26, 2011 at 8:59

2 Answers 2

4
+50

I was in the middle of writing a pretty through response, and then I found someone already wrote the same thing!!

Here you go: http://trevorturk.com/2008/12/11/easy-upload-via-url-with-paperclip/

This should be exactly what you are looking for. If you stumble, let me know.

Good luck

Sign up to request clarification or add additional context in comments.

1 Comment

I have tried that solution. It would be nice if you could answer this question: stackoverflow.com/questions/6180808/…
2

It seems like you are trying to access a parameter that doesn't match what you have in the form:

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


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

Should you be using :photo_url instead of :photo_remote_url in the controller?

3 Comments

Hard to debug this without being able to run the code, sorry!
@Rails beginner Looking at it some more, could you have the new and create methods confused in the controller? I think that create is what gets the info after the form submit, so the code to set the Remote upload should probably be in create, not new.
If that doesn't help, start putting puts statements in the controller methods to verify the code is at least being called with the inputs you expect. Would help narrow this down. Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.