views:

29

answers:

1

I have a photo rails app using paperclip. My model called photo and the request parameter also photo.So when i am trying to upload via curl i use : curl -F "photo[photo]=@/pics/pic.jpg" http://something/photos.xml . That's works fine! How can i change the photo[photo] parameter to "photo" or "media" or something else? How can i change the endpoint url? (ex. http//something/upload.xml)

thanx! any help will be highly appreciated, :-)

A: 

What you could do is setup another controller and work with it. What paperclip does is just setup an extra "photo" attribute and methods, thus reusing Rails .new and .update_attributes own methods. That way, when you call /photos.xml with that info, what you are doing is just a regular post photo action, with the added benefit of setting up it's picture.

When you do Object.photo = YOUR_PHOTO, you are actually using Paperclip code.

So, you could work with something like:

class ApplicationController < ActiveController::Base

 def upload
  photo = Photo.new
  photo.photo = params[:photo]
  # ... extra code
  photo.save
  render :text => "Ok"
 end

end

And add a route like:

map.upload "/upload(.:format)", :controller => "application", :action => "upload" 

(or it's Rails3 equivalent if you are using it)

That way, when you do 'curl -F "photo=@/pics/pic.jpg" http://something/upload.xml', you will invoke the upload action and create a photo using the 'photo' parameter. The photo = params[:photo] will take the tempfile you've uploaded and continue with the usual paperclip tasks :)

Yaraher
This is very useful, it helps me a lot. Thanks! ;-)
vic
Glad I could be of help :)
Yaraher