Newbie Post

Greets to the list,

I just started learning RoR, coming from a PHP/MYSQL background. So
this is all new for me…

I have a project where I need to upload a picture into my db.

I have been reading the wiki (I wish it was more complete) and have
gleaned the code to upload directly into a database.

My form is setup correctly, as I can insert data into the DB without
issues. Now I am trying to store a jpg coming from the form field
listings[pic]. When I submit the form, I get this error:


NoMethodError in Admin#create

undefined method `filename=’ for #Listing:0xb7916be0

RAILS_ROOT: script/…/config/…

Code is at the bottom, I know it’s probrally a mess, but I’ve only
been at this for a few days. :slight_smile:

Any insight would be great.
Thanks

~kurth

–code–
def create
@params[‘listing’][‘filename’] =
@params[‘listing’][‘pic’].original_filename.gsub(/[^a-zA-Z0-9.]/, ‘_’)
@params[‘listing’][‘picture’] = @params[‘listing’][‘pic’].read
@params[‘listing’].delete(‘pic’)

@listing = Listing.new(params[:listing])

if @listing.save
  flash[:notice] = 'Listing was successfully created.'
  redirect_to :action => 'list'
else
  render :action => 'new'
end
#redirect_to :action => "show", :id => object_id

end

Do you have a field in your database called “filename”? If not, add
that field to the db (presumably varchar 255) and you should be set.

Without that field when you write:

@listing = Listing.new(params[:listing])

Rails attempts to make a new Listing object and set an attribute named
“filename” but the model (looking at the database) doesn’t think a
Listing object has an attribute called “filename”, hence when you try
to set it, it fails with a NoMethodError. This can be confusing if you
are not familiar with object oriented programming and Ruby. I know it
was for me, coming from PHP as you are.

You may also wish to investigate the FileColumn plugin if you are doing
lots of image work. I know I found it useful for some cases. Take a
look here:

http://wiki.rubyonrails.com/rails/pages/HowToUseFileColumn

You will need to do some additional configuration, like installing the
plugin and rmagick, among other things. That page and the following
have some instructions for win32 and Linux/BSD:

http://rmagick.rubyforge.org/install-faq.html

Using Debian I was able to apt-get install several packages instead of
compiling, but ymmv.


The wiki is a bit incomplete and as a PHP/MySQL refugee myself, I do
miss the old php.net site and its function lookups with comments. My
SAMS book taught me PHP but php.net made me effective. Rails and the
surrounding community is young, all I can say is give it time. If you
want to help, you could figure out a few PHP equivalents and put them in
the wiki here:

http://wiki.rubyonrails.com/rails/pages/PHPEquivalents

Of course, you may want to dig a bit more into Ruby (and Rails) before
trying something like that. I personally found Rails relatively easy
after I worked through the “Four Days on Rails” tutorial and part of the
“Agile Web D. with Rails” book. The most difficult part for me
was figuring out how to do stuff in Ruby that I was used to doing in PHP
like array and functions, looping, iterators and other not-rails stuff.
The first few chapters of the “Programming Ruby” (AKA the “Pickaxe”)
book and its function reference in the rear proved invaluable for this.

Good luck!

Nicholas P. Mueller