So at every submit request my rails server shows it is trying to get the logo and then shows there is no user with
id = logo.png
Started GET "/users/2" for 127.0.0.1 at 2016-10-02 19:01:18 +0530
Processing by UsersController#show as HTML
User Load (0.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1
User Load (0.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1
Rendered users/show.html.erb within layouts/application (1.8ms)
Rendered layouts/_header.html.erb (0.0ms)
Rendered layouts/_sidebar.html.erb (1.0ms)
Completed 200 OK in 130ms (Views: 126.9ms | ActiveRecord: 0.5ms)
Started GET "/users/logo.png" for 127.0.0.1 at 2016-10-02 19:01:18 +0530
Processing by UsersController#show as PNG
User Load (1.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 0 LIMIT 1
Completed 404 Not Found in 3ms (ActiveRecord: 1.0ms)
ActiveRecord::RecordNotFound (Couldn't find User with 'id'=logo):
get 'signup' => 'users#new'
get 'admins' => 'users#admins_dashboard'
delete 'users' => 'users#destroy'
get '/users/edit/:id' => 'users#edit'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
@user = User.find(params[:id])
@user = User.new(user_params)
session[:user_id] = @user.id
class User < ActiveRecord::Base
self.role == 'admin'
This happened due to the way the browser resolves relative urls. When you use a fully qualified url for an image src(eg: http://example.com/logo.png), the browser doesn't have to do anything. It just issues a request to that URL. However, when you use relative URLs(without the protocol and domain), the browser has to figure out the full URL to issue the request.
<img alt="Brand" src="logo.png" /> in your layout, so the browser used the current context to resolve the full URL for that image. When you were on
http://example.com/users/1, the src was resolved to
http://example.com/users/logo.png because it used the current path. When you were in the homepage the logo could be found because you were at the root path, so it resolved that src to
One way to solve this is to prepend a slash to the src
<img alt="Brand" src="/logo.png" /> forcing the browser to resolve it at the root of your domain. That will only work in your rails app if that image is on the public folder.
If you placed the image inside your assets, it's best to use rails image_tag helper.