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"
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.
I tried this and got an error.
My stuff was based on this website -
Ruby on Rails - File Uploading.
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.
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.