Currently I'm using Paperclip to handle my avatar uploads and usage in Rails 3. I have a Users resource (With controller, model, views etc.) where I'm uploading the avatars to (using the users/new view) and they actually upload and work when "embedding" them in the Users show View for example. (I get broken images, but I assume is because of the local server as the images actually save and link to the correct path in my app when looking at the source code) Now, I have another resource called skills (with controller, model and views also) In wich views I need the avatars to show too, but whenever I try to embed/use an user avatar in a skills view I get the following error (The same thing if I try to embed the avatars in another view different from users/show):

undefined methodavatar' for nil:NilClass`

Why is this happening?

Also, as I say, when embeding the avatars in the show view, they actually "embed" but I get broken images and the following Routing error:

No route matches [GET] "/public/assets/users/UserID/thumb/userimage.jpg"

I'm on Localhost... Is because of that? (I'm guessing so because "userimage" it's actually saved on that path in the app)

Could someone explain me what's happening with this routing error and how to use the avatars in multiple views? I'm "embedding" the images with the following code:

<%= image_tag @user.avatar.url(:thumb) %>

Thank You.

有帮助吗?

解决方案

Fixed the problems! For the

undefined methodavatar' for nil:NilClass`

I removed the "@" from user in the following code:

<%= image_tag @user.avatar.url(:thumb) %>

Then, for the following error:

No route matches [GET] "/public/assets/users/UserID/thumb/userimage.jpg"

I had to remove the "public" from my Url Symbol in the user model, so it changed from this:

:url  => "/public/assets/users/:id/:style/:basename.:extension"

To this:

:url  => "/assets/users/:id/:style/:basename.:extension"

Now the images show (in local and production enviroments) and I'm able to use them from all the views I need.

Hope this helps somebody.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top