Search code examples
rubyimageruby-on-rails-5

Why does my logo fail to load after logging into Rails App?


My logo loads on my RoR application locally. When I login however, it fails to load each time. If however, I redirect to the same page, it will load again. I'm not sure I understand what the problem is.

The link to my project can be found here https://github.com/dlyons8/family-and-community.


Solution

  • The logo cannot be loaded because your path is being changed after successfully logging in as an user. The Sessions controller will be redirect the user to /users/:user_id.

    The affected src attribute in the <img> tag contains "logo.png" so the browser tries to fetch /users/logo.png which will not succeed.

    You have two options:

    1) Change your logo as an asset. This is commonly used pattern for resources and helps a lot later in deployment when you change the logo for example. Move logo.png to app/assets/images and in the app/views/layouts/application.html.erb use the image_tag helper:

    <%= image_tag 'logo.png', alt: 'Logo' %>
    

    In this way, you will always get the absolute path of your logo (/assets/logo.png) and don't have to worry about where is the user actually.

    2) Change the path in the <img> tag to absolute path of the logo (/logo.png) and it will try to fetch the correct file.

    The 2) solution would be easier, however I strongly recommend you to get in touch with the Asset Pipeline in Rails because it really helps a lot in handling static assets for your webpage.

    Also, if you encounter a non-loading asset, always check the browser's console. In Google Chrome, you can reach it by simply pressing F12 and switching to Console tab, there will be a red error message about what resource could not be loaded and why. The displayed full URL is often helps in these sneaky problems.