Unable to access Paperclip Avatar globally

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? %>

  • <%= image_tag(@user.avatar.url(:thumb), unless: avatar.nil?) %>
  • 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?

    On Fri, Jun 26, 2015 at 7:20 AM, David W. [email protected]
    wrote:

    unless: avatar.nil?) %>

    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 )

    Nugi Nugraha wrote in post #1175693:

    On Fri, Jun 26, 2015 at 7:20 AM, David W. [email protected]
    wrote:

    unless: avatar.nil?) %>

    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 )

    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>:

    On 26 June 2015 at 16:40, David W. [email protected] wrote:

    “current_user.avatar.url(:thumb)”

    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>:

    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

    On 27 June 2015 at 15:25, David W. [email protected] wrote:

    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

    Colin L. wrote in post #1175739:

    On 26 June 2015 at 16:40, David W. [email protected] 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.

    This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

    | Privacy Policy | Terms of Service | Remote Ruby Jobs