# Rails+sinatra sent image

**URL:** <https://www.ruby-forum.com/t/rails-sinatra-sent-image/189867>\
**Category:** Rails\
**Created:** [June 9, 2010, 7:52am UTC](https://www.ruby-forum.com/t/rails-sinatra-sent-image/189867 "2010-06-09T07:52:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bebesuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dfb087/32.png) [@bebesuk](https://www.ruby-forum.com/u/bebesuk)\
**Post date:** [June 9, 2010, 7:52am UTC](https://www.ruby-forum.com/t/rails-sinatra-sent-image/189867/1 "2010-06-09T07:52:17Z")

</div>

Hi!  
I have simple rails application - users with name and avatar and in  
sinatra I want to create new user.

form for new user

Name:

Avatar:

My action in sinatra

post ‘/users/create’ do  
RestClient.post(“[http://localhost:3000/users](http://localhost:3000/users)”,  
:user=\>params[‘user’])  
redirect ‘/users’  
end

I would like to sent file to server in this action.

So the question is: How to sent file as a param to server ?  
I’m using mozilla firefox 3.6 so I can’t get full file path to read it

---

<div class="post-metadata">

**Author:** ![bebesuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dfb087/32.png) [@bebesuk](https://www.ruby-forum.com/u/bebesuk)\
**Post date:** [June 9, 2010, 10:28am UTC](https://www.ruby-forum.com/t/rails-sinatra-sent-image/189867/2 "2010-06-09T10:28:24Z")

</div>

On Jun 9, 6:52 am, Bohdan P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> post ‘/users/create’ do  
> RestClient.post(“[http://localhost:3000/users](http://localhost:3000/users)”,  
> :user=\>params[‘user’])  
> redirect ‘/users’  
> end
> 
> I would like to sent file to server in this action.
> 
> So the question is: How to sent file as a param to server ?  
> I’m using mozilla firefox 3.6 so I can’t get full file path to read it

Looks like you can pass a :multipart option to RestClient to tell it  
that the post should be multipart.

Fred

---

<div class="post-metadata">

**Author:** ![bebesuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dfb087/32.png) [@bebesuk](https://www.ruby-forum.com/u/bebesuk)\
**Post date:** [June 9, 2010, 11:14am UTC](https://www.ruby-forum.com/t/rails-sinatra-sent-image/189867/3 "2010-06-09T11:14:23Z")

</div>

I tried next

post ‘/users/create’ do  
RestClient.post(“[http://localhost:3000/users](http://localhost:3000/users)”, :user=\>params[‘user’],  
:avatar=\>File.read(“/home/qwerty1/Desktop/client/views/images.jpeg”),  
:multipart=\>true)  
redirect ‘/users’  
end

* * *

require ‘paperclip’  
class User \< ActiveRecord::Base  
has\_attached\_file :avatar, :styles=\>{:thumb=\>“100x100”,  
:medium=\>“200x200”}

# validates\_attachment\_presence :avatar

end

but id didn’t sent any image to server

Am I doing something wrong ?

---

<div class="post-metadata">

**Author:** ![bebesuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dfb087/32.png) [@bebesuk](https://www.ruby-forum.com/u/bebesuk)\
**Post date:** [June 9, 2010, 11:33am UTC](https://www.ruby-forum.com/t/rails-sinatra-sent-image/189867/4 "2010-06-09T11:33:58Z")

</div>

On Jun 9, 10:14 am, Bohdan P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> require ‘paperclip’  
> class User \< ActiveRecord::Base  
> has\_attached\_file :avatar, :styles=\>{:thumb=\>“100x100”,  
> :medium=\>“200x200”}
> 
> # validates\_attachment\_presence :avatar
> 
> end
> 
> but id didn’t sent any image to server
> 
> Am I doing something wrong ?

Did you try setting a breakpoint in your rails app to see that the  
image was being received properly. Does paperclip expect the name of  
the uploaded file (which you aren’t currently transmitting) to have an  
acceptable extension or for the content type to be set ?

Fred

---

<div class="post-metadata">

**Author:** ![bebesuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/dfb087/32.png) [@bebesuk](https://www.ruby-forum.com/u/bebesuk)\
**Post date:** [June 9, 2010, 3:18pm UTC](https://www.ruby-forum.com/t/rails-sinatra-sent-image/189867/5 "2010-06-09T15:18:40Z")

</div>

Solved 🙂

#POST create  
post ‘/users/create’ do  
f = File.new(“temp#{params[:user][:login]}.jpg”, “w”)  
t = params[:avatar][:tempfile]  
while c = t.gets do  
f.puts(c)  
end  
f.close  
RestClient.post(“[http://localhost:3000/users](http://localhost:3000/users)”,:user=\>{  
:login=\>params[:user][:login],  
:email=\>params[:user][:email],  
:name=\>params[:user][:name],  
:avatar=\>File.new(“temp#{params[:user][:login]}.jpg”)},  
:multipart=\>true)  
File.delete(“temp#{params[:user][:login]}.jpg”)  
redirect ‘/users’  
end
