Re: [ruby-81] how to post image data over using ActiveResource

From: Elliott B.
Sent on: Friday, June 6, 2008 11:31 AM
Alex,

I was able to finally find a solution which incorporated your ideas.
If anyone is interested, see this url:

http://benr75.com/articles/2008/01/04/attachment_fu-now-with-local-file-fu

Thanks again,

Elliott

?

----- Original Message ----
From: Alex Wayne <[address removed]>
To: [address removed]
Sent: Thursday, June 5,[masked]:56:02 PM
Subject: Re: [ruby-81] how to post image data over using ActiveResource

I would use base 64 encoding. ?Base 64 encodes binary data as text, with a vocabulary of 64 characters.

You controller shouldnt need to change at all:

def create
??Asset.create(params[:asset])
end

If you post a base 64 encoded file as a param, you can decode it in your model and save it somewhere:

require 'base64'
class Asset
??def file_encoded=(base64_data)
?? ?data =?Base64.decode64(base64_data)
?? ?# save data somewhere...
??end
end

On the client side, you can encode with

require 'base64'
encoded_file = Base64.encode64(my_file.read)


I've seen many API's use this to send images as part of the XML response. ?No reason you couldn't post them too.
-Alex

On Jun 5, 2008, at 2:59 PM, elmo wrote:

Hi,

Thanks for your ?reply, ?but?I'm trying to solve a different problem. ? To explain it better than I have, consider the following controller snippet. ?It works fine when accessed via a traditional HTML form. ?What I want to do is access this same action but a different respond_to block via XML -- which is to say, I want to do POST to /parents/1/assets.xml ?and have params[:asset] be set correctly as to allow the creation of a new asset resource on my service.

Here is the REST service controller create action:

??def create
?? ?@parent = parent_object
?? ?@asset = @parent.assets.new(params[:asset])
?? ?respond_to do |format|
?? ? ?if @asset.save
?? ? ? ?flash[:notice] = 'Asset was successfully created.'
?? ? ? ?format.html { redirect_to parent_assets_path }
?? ? ? ?format.xml ?{ render :xml => @asset, :status => :created, :location => @asset }
?? ? ?else
?? ? ? ?format.html { render :action => "new" }
?? ? ? ?format.xml ?{ render :xml => @asset.errors, :status => :unprocessable_entity }
?? ? ?end
?? ?end
??end

The crux of the problem, is that in my feeding/consuming application,?I'd like to formulate the correct ActiveResource call which should be something like this:

@asset = Asset.new( :parent_id => @parent.id , :uploaded_data => ??????)
@asset.save

Is that any more clear?





--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
This message was sent by elmo ([address removed]) from The East Bay Ruby Meetup Group.
To learn more about elmo, visit his/her member profile
To unsubscribe or to update your mailing list settings, click here

Meetup Support: [address removed]
632 Broadway New York NY 10012 USA





--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
This message was sent by Alex Wayne ([address removed]) from The East Bay Ruby Meetup Group.
To learn more about Alex Wayne, visit his/her member profile
To unsubscribe or to update your mailing list settings, click here

Meetup Support: [address removed]
632 Broadway New York NY 10012 USA

Our Sponsors

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy