andrzej541
andrzej541

Reputation: 961

Rails: path-string changed automatically in the view

So I found this weird-looking bug in my app. I call user avatar as a background image like this:

<span class="user-image" style="background-image:url(<%= avatar_tiny(current_user) %>);"></span>

avatar_tiny is a helper which should returns a path to user's avatar or a path to the default image:

def avatar_tiny(user)
    if user.contact.avatar?
      user.contact.avatar.url(:tiny)
    else
     'assets/avatar_default_tiny.png'
    end
  end

It works correct on the home page. But when I go to the any subpage Rails somehow change automatically this path adding it's name to this string. E.g. "/profile/assets/avatar_default_tiny.png

And I get this error in my rails console:

Started GET "/profile/assets/avatar_default_tiny.png" for 127.0.0.1 at 2019-02-10 05:32:51 +0100

ActionController::RoutingError (No route matches [GET] "/profile/assets/avatar_default_tiny.png"):

Why this is happen and how can I fix it?

Upvotes: 0

Views: 41

Answers (1)

Giorgio Zanni
Giorgio Zanni

Reputation: 569

You should use the asset_path helper to get the relative location of your default image. Change your helper's else statement to asset_path('avatar_default_tiny.png')

Upvotes: 2

Related Questions