I am new to rails and am stuck on the above.
I have used a mixture of code from the agile web dev with rails book
and what I have found with google.
I realise there are easier ways to do this with various plugins but I
would be interested to know why my code does not work.
The code runs and a file is created in the expected place but this
file is zero length so something is wrong.
Model code
class Photo < ActiveRecord::Base
belongs_to :user
validates_format_of :content_type,
:with => /^image/,
:message => "--- you can only upload
pictures"
def uploaded_picture=(picture_field)
self.photofile = base_part_of
(picture_field.original_filename)
self.content_type = picture_field.content_type.chomp
self.is_visible = 0
@data = picture_field.read
end
etc etc
Controller code
def create
@photo = Photo.new(params[:photo])
filename = @photo.photofile
directory = "public/images"
# create the file path
user= User.find(session[:userid])
path = File.join(directory, user.user_id)
if File.exist?(path) == false
Dir.mkdir(path)
end
path = File.join(directory, user.user_id, filename)
# write the file
# File.new(path, "wb") { |f| f.write(params[:picture_field].read) }
File.new(path, "wb") { |f| f.write(@data) }
and the view
<% form_for(:photo,
:url => {:action => ''create''},
:html => { :multipart => true }) do |f| %>
<p>
<b>Caption</b><br />
<%= f.text_field :caption %>
</p>
Upload your picture: <%= f.file_field("uploaded_picture")
%><br/>
<p>
<%= f.submit "Upload Photo" %>
</p>
<% end %>
So there are obviously some basics here that I have not yet "got".
Many thanks for looking
--~--~---------~--~----~------------~-------~--~----~
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
it seems you''re trying to write @data into that file. you didn''t initialize that variable in your controller. the only occurrence is inside your model. but model and controller don''t share variables (even if they have a similar name). in such a case it is always helpful to debug your code. set a debugger and see which variables contain which values. also: don''t try to call methods (in your model) from your view. in general i''d advice to stick to MVC-pattern. you could put that uploaded_picture-code into your helper or just recode it. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
for your view, if in doubt use this instead of <%= f.file_field
("uploaded_picture") %>
<input type="file" name="chosen_file" />
in your controller (the line you commented out):
File.new(path, "wb") { |f| f.write(params[:chosen_file].read) }
if i didn''t miss anything, this should do the trick.
--~--~---------~--~----~------------~-------~--~----~
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
I tried this and got an error. My stuff was based on this website - http://www.tutorialspoint.com/ruby-on-rails/rails-file-uploading.htm. I wanted a photo stored in images/user-id/ whereas this only uses images/. So if I remove the user-id directory, and do this, I still get an empty file. Thanks for you replies - I need to think my rails usage. On Mar 3, 12:11 pm, MaD <mayer.domi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> for your view, if in doubt use this instead of <%= f.file_field > ("uploaded_picture") %> > <input type="file" name="chosen_file" /> > > in your controller (the line you commented out): > File.new(path, "wb") { |f| f.write(params[:chosen_file].read) } > > if i didn''t miss anything, this should do the trick.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
well, you got a few errors here and there. for example: path = File.join(directory, user.user_id, filename) i guess user doesn''t have a user_id but an id, so try: path = File.join(directory, user.id, filename) if you post your error-message, we can try to get there step by step. but in general it would be best if you followed some tutorials for your first steps. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---