Unable to access Paperclip Avatar globally

30 views
Skip to first unread message

David Williams

unread,
Jun 25, 2015, 8:21:28 PM6/25/15
to rubyonra...@googlegroups.com
In my application.html.erb, I have a navigation bar that stores the user
avatar. He or she should be able to return to the main index or traverse
through the different controllers without issue. I seem to be getting a
"Cannot find avatar for type ActiveRecord::Relation"

undefined method `avatar' for nil:NilClass

Application.html.erb
<%= if user_signed_in? %>
<li class="round-image-50"><%= image_tag(@user.avatar.url(:thumb),
unless: avatar.nil?) %></li>


User.rb

has_attached_file :avatar, :styles => { :medium => "200x200>", :thumb =>
"100x100>" }, :unless => "avatar.blank?",
:url =>
"/assets/users/:id/:style/:basename.:extension",
:path =>
":rails_root/public/assets/users/:id/:style/:basename.:extension"

validates_attachment_content_type :avatar, :content_type =>
/\Aimage\/.*\Z/

The avatar uploads successfully when the user registers, and the file
itself is attached to the user object.


Do you have a solution for this problem? And how would you enable the
app to show all user avatars globally?

--
Posted via http://www.ruby-forum.com/.

Nugi Nugraha

unread,
Jun 25, 2015, 11:50:40 PM6/25/15
to rubyonra...@googlegroups.com
On Fri, Jun 26, 2015 at 7:20 AM, David Williams <li...@ruby-forum.com> wrote:
In my application.html.erb, I have a navigation bar that stores the user
avatar. He or she should be able to return to the main index or traverse
through the different controllers without issue. I seem to be getting a
"Cannot find avatar for type ActiveRecord::Relation"

undefined method `avatar' for nil:NilClass

Application.html.erb
<%= if user_signed_in? %>
<li class="round-image-50"><%= image_tag(@user.avatar.url(:thumb),
unless: avatar.nil?) %></li>

I think error message is obvious
you got that error because @user was nil
if you're using devise, you can try this instead "current_user.avatar.url(:thumb)"


--
Mou Dareka no, tame janakutte
Jibun no Tame ni Warette Iru

( Aqua Timez - Alones )

David Williams

unread,
Jun 26, 2015, 11:41:53 AM6/26/15
to rubyonra...@googlegroups.com
Nugi Nugraha wrote in post #1175693:
I'm getting this error in my logs file. The current_user is signed in
btw.

User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ?
ORDER BY "users"."id" ASC LIMIT 1 [["id", 3]]
Completed 500 Internal Server Error in 2302ms (ActiveRecord: 2.0ms)

NameError - undefined local variable or method `avatar' for
#<#<Class:0x58867e0>:0x5885d78>:

Colin Law

unread,
Jun 26, 2015, 12:13:08 PM6/26/15
to rubyonra...@googlegroups.com
Is that the same error (undefined method `avatar' for nil:NilClass) or
a new one? If a new one then show us the full error message and the
line of code it relates to. If the same error then, as the previous
poster said, it is because @user is nil.

Also what is the code
unless: avatar.nil?
supposed to be doing? I see no code setting up a variable avatar.

Colin

David Williams

unread,
Jun 27, 2015, 10:26:16 AM6/27/15
to rubyonra...@googlegroups.com
Colin Law wrote in post #1175739:
> On 26 June 2015 at 16:40, David Williams <li...@ruby-forum.com> wrote:
>>> "current_user.avatar.url(:thumb)"

> Also what is the code
> unless: avatar.nil?
> supposed to be doing? I see no code setting up a variable avatar.
>
> Colin

I fixed it! - Thanks, anyway.

Colin Law

unread,
Jun 27, 2015, 11:04:58 AM6/27/15
to rubyonra...@googlegroups.com
On 27 June 2015 at 15:25, David Williams <li...@ruby-forum.com> wrote:
> Colin Law wrote in post #1175739:
>> On 26 June 2015 at 16:40, David Williams <li...@ruby-forum.com> wrote:
>>>> "current_user.avatar.url(:thumb)"
>
>> Also what is the code
>> unless: avatar.nil?
>> supposed to be doing? I see no code setting up a variable avatar.
>>
>> Colin
>
> I fixed it! - Thanks, anyway.

It is good practice to write what the problem was, so that others
finding this thread may benefit from your experience.

Colin
Reply all
Reply to author
Forward
0 new messages