0

I've installed the filepicker-rails gem and I am able to upload files to the filepicker.io server. When I try to display the image for different users profile, I get the error:

undefined method 'avatar_url' for nil:NilClass

Under my users directory, in the show.html.erb file I have:

<%= filepicker_image_tag @user.avatar_url, w: 160, h: 160, fit: 'clip' %>

I have the following under my User.rb file:

def avatar_url(user)
  user.avatar_url
end

Any ideas why this doesn't work?

Chris Morgan
  • 86,207
  • 24
  • 208
  • 215
thewheelz
  • 379
  • 2
  • 15

1 Answers1

0

Basically you'll need a column in your DB called avatar_url. Write a migration that will give you a avatar_url column as just a regular string column. Since you're using Rails, ActiveRecord will provide the avatar_url method for you.

Edit: In your controller you're likely not looking up your user correctly which is resulting in a method call on a NilClass in your view.

Shane O'Connor
  • 704
  • 6
  • 14
  • Shane, I already added the `avatar_url` into the migration. I get the same error... – thewheelz Sep 27 '12 at 22:04
  • Ah, my apologies, I didn't read this correctly initially. `undefined method 'avatar_url' for nil:NilClass` indicates that your `@user` is empty. So in your controller, you're either not looking up your `User` correctly, or not at all. You're trying to call a method on an object that doesn't exist, essentially. Hope that helps. – Shane O'Connor Sep 27 '12 at 23:38
  • Yes, before posting my issue, I tried defining the method 'show' with: `@user = User.find(params[:id])`. That, unfortunately gives me the following error: `Couldn't find User without an ID` – thewheelz Sep 28 '12 at 02:15
  • Ah, you'd have to post more code then, likely view code. For whatever reason, `params[:id]` seems to be empty, so it's trying to do a find without an id. – Shane O'Connor Sep 28 '12 at 02:39
  • Shane, actually that was exactly the problem. My 'show' method had `@user = User.find(params[:id])`. However, when I changed it to: `@user = User.find(session[:user_id])` it worked! – thewheelz Sep 28 '12 at 03:11