Forum: Ruby on Rails upload file to save

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
A7759219776f7cf59fd8cb9104b7f037?d=identicon&s=25 norricorp (Guest)
on 2009-02-27 16:38
(Received via mailing list)
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
    self.content_type = picture_field.content_type.chomp
    self.is_visible = 0
    @data =
etc etc

Controller code
  def create
      @photo =[: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
      path = File.join(directory, user.user_id, filename)
    # write the file
#, "wb") { |f| f.write(params[:picture_field].read) }, "wb") { |f| f.write(@data) }

and the view
<% form_for(:photo,
            :url => {:action => 'create'},
            :html => { :multipart => true }) do |f| %>

    <b>Caption</b><br />
    <%= f.text_field :caption %>
  Upload your picture: <%= f.file_field("uploaded_picture") %><br/>
    <%= f.submit "Upload Photo" %>
<% end %>

So there are obviously some basics here that I have not yet "got".

Many thanks for looking
80e4cb97cae5c8d745f72337d93fd8f2?d=identicon&s=25 MaD (Guest)
on 2009-03-03 12:49
(Received via mailing list)
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.
80e4cb97cae5c8d745f72337d93fd8f2?d=identicon&s=25 MaD (Guest)
on 2009-03-03 13:12
(Received via mailing list)
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):, "wb") { |f| f.write(params[:chosen_file].read) }

if i didn't miss anything, this should do the trick.
A7759219776f7cf59fd8cb9104b7f037?d=identicon&s=25 norricorp (Guest)
on 2009-03-06 13:07
(Received via mailing list)
I tried this and got an error.
My stuff was based on this website -
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.
80e4cb97cae5c8d745f72337d93fd8f2?d=identicon&s=25 MaD (Guest)
on 2009-03-06 13:18
(Received via mailing list)
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,, 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.
This topic is locked and can not be replied to.