I'm trying to add the avatar upload functionality in Ruby on rails
following the railsspace book.
Before I got the code and re-wrote it for my own needs, I wanted to
make sure everything worked anyway, so I added the code from the book
to the avatar controller etc..
When I attempted an upload, I got an error: NoMethodError in
AvatarController#upload
So, when I look at the upload function, all seems OK to me. I'm
running rails 2.3.3 and have managed to get past every issue so far
without reverting back to an earlier version.
def upload @title = "Upload Your Avatar" @user = User.find(session[:user_id])
if param_posted?(:avatar)
image = params[:avatar][:image] @avatar = Avatar.new(@user, image)
if @avatar.save
flash[:notice] = "Your avatar has been uploaded."
redirect_to hub_url
end
end
end
Originally it had a problem because of this:
def index
# redirect_to hub_url
end
The error with this code in place reads: undefined local variable or
method `hub_url. This is displayed upon accessing url : http://localhost:3000/avatar
Can anyone help?
I might look for another tutorial to upload avatars, but was hoping
there would be a simple solution..
The error with this code in place reads: undefined local variable or
method `hub_url. This is displayed upon accessing url :http://localhost:3000/avatar
Can anyone help?
Nothing to do with your upload function itself - the hub_url method
gets created when you declared a named_route called hub in your
routes.rb - your book probably describes that in more detail if you
need it
You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.delete
NoMethodError in AvatarController#upload
You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.delete
You probably could have included that error in your post. I don't
see .delete in your method, so I'm guessing it's somewhere deeper.
Typically, error messages have line number, so that's something to
check.
- Railsspace isn't the best tutorial to start with - the ideas are
good, but trying to update a 1.2.3-era tutorial to present isn't the
most helpful. It also re-implements a lot of things that have mostly
been rolled into popular plugins (authentication for one; also this
whole image upload bit).
- In general, always post as much backtrace from an error as you can.
The final error is sometimes really just where the code fell down
after wandering in the woods for a while.
- Similarly, if you're troubleshooting a controller action, posting
the log output for the action is handy as well; I've chased more than
one bug in my own code that came down to a missing method= on a form,
which shows up immediately in the log.
- You are almost certainly missing something from the earlier chapters
of Railsspace - I found the relevant discussion of routing (re:
hub_url) in Chapter 9, Listing 9.39.