Ouch! Can't figure out this file upload issue...

Okay, so this is driving me crazy. I'm trying to do a file upload
using code from a book.

What happens:
I get wrong number of arguments (1 for 0) in the upload controller
save action. No idea why.

Schema:
The schema for the picture model object is just a textfield named
comments and a binary field called data.

Upload controller:

  def get
    @picture =Picture.new
  end

  def save
    @picture=Picture.new(params[:picture])
    if @picture.save
      redirect_to(:action=>'show', :id=> @picture.id)
    else
      render(:action=> :get)
    end
  end

  def picture
    @picture=Picture.find(params[:id])
    send_data(@picture.data,
              :filename=>@picture.name,
              :type=>@picture.content_type,
              :disposition=>"inline")
  end

  def show
    @picture=Picture.find(params[:id])
  end

Views:
For Get--
<h1>Upload Image File</h1>

<% form_for(:picture,
            :url=> {:action=>'save'},
            :html=> { :multipart=> true}) do |f| %>

  <%= f.error_messages %>

  <p>
    <%= f.label :comment %><br />
    <%= f.text_area :comment %>
  </p>
  <p>

    <%= f.file_field("uploaded_picture")%>
  </p>
  <p>
    <%= f.submit "Create" %>
  </p>
<% end %>

Then there's a show view that's not really worth including, because it
is standard and nowhere near the error.

What am I doing wrong?

Thanks!

Oh and the model code is:

class Picture < ActiveRecord::Base
  validates_format_of :content_type,
  :with=>/^image/,
  :message=>'Uploading is limited to pictures'

  def uploaded_picture=(picture_field)
    self.name = base_part_of(picture_field.original_filename)
    self.content_type = picture_field.content_type.chomp
    self.data = picture.field.read
  end

  def base_part_of
    File.basename(file_name).gsub(/[^\w._-]/,'')
  end
end

Ron wrote:

Oh and the model code is:

class Picture < ActiveRecord::Base
  validates_format_of :content_type,
  :with=>/^image/,
  :message=>'Uploading is limited to pictures'

  def uploaded_picture=(picture_field)
    self.name = base_part_of(picture_field.original_filename)
    self.content_type = picture_field.content_type.chomp
    self.data = picture.field.read
  end

  def base_part_of
    File.basename(file_name).gsub(/[^\w._-]/,'')
  end
end

Looks like this is the problem:
self.name = base_part_of(picture_field.original_filename)

The function does not accept any arguments.

Hi,

So I'm following along with Agile Development with Rails, 2nd edition.

I got the code from pg. 503. I'm not saying this because I think its
right--I'm just saying it because if it doesn't take an argument, I'm
not sure how else to do it....

Ron

Ron wrote:

Hi,

So I'm following along with Agile Development with Rails, 2nd edition.

I got the code from pg. 503. I'm not saying this because I think its
right--I'm just saying it because if it doesn't take an argument, I'm
not sure how else to do it....

Ron

On Jun 14, 10:02 pm, Chris Olsen <rails-mailing-l...@andreas-s.net>

On page 503 the base_part_of function accepts a filename argument.

I think someone has had too much coffee :slight_smile:

I think you could be right about the coffee (although its green tea in
my case), Chris. Just as an aside, do you know what disposition
inline means under send_data on the upload controller?

Thanks for the help!

Ron

Ron wrote:

Just as an aside, do you know what disposition
inline means under send_data on the upload controller?

The inline option is what allow the image to be rendered on the page
rather than downloaded. If instead you set the disposition to
"attachment" it should prompt you to save/open the file.

Thanks for the help!

Anytime :slight_smile:

Ah! That makes sense.

So my typo was behind the arguments issue...but now that it uploads
it, there is a problem that 'picture' is an undefined local variable
or method in the save action of the upload controller. I'm not sure
why because I defined a picture method for in the picture model. Is
it an issue of scope?

R

Whoops...I'm the world's worst typist. It turns out that it was just
a typo in the model...picture.field instead of picture_field. Really
need to drink fewer stimulants...
Ron