Hi!
I have simple rails application - users with name and avatar and in
sinatra I want to create new user.
form for new user
<form action="/users/create"
enctype="multipart/form-data"
method="post">
<p>
<label for="name">Name: </label>
<input type="text" id="name"
name="user[name]" >
</p>
<p>
<label for="avatar">Avatar: </label>
<input type="file" id="avatar"
name="avatar" >
</p>
<input type="submit" name="Commit"
value="Create">
</form>
My action in sinatra
post ''/users/create'' do
RestClient.post("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
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jun 9, 6:52 am, Bohdan Pohoriletz <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> post ''/users/create'' do > RestClient.post("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 itLooks like you can pass a :multipart option to RestClient to tell it that the post should be multipart. Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I tried next
post ''/users/create'' do
RestClient.post("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 ?
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jun 9, 10:14 am, Bohdan Pohoriletz <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I tried next > > post ''/users/create'' do > RestClient.post("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 ?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> -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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",: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
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.