defiant defiant - 2 months ago 9
Ruby Question

Rails app tries to GET "/users/logo.png" and shows ActiveRecord::RecordNotFound (Couldn't find User with 'id'=logo) with every submit

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
. Can somebody please help me!

Detailed error message:

Started GET "/users/2" for 127.0.0.1 at 2016-10-02 19:01:18 +0530
Processing by UsersController#show as HTML
Parameters: {"id"=>"2"}
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
Parameters: {"id"=>"logo"}
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):
app/controllers/users_controller.rb:43:in `show'


Here is my routes file:

Rails.application.routes.draw do
root 'dashboard#show'

get 'signup' => 'users#new'
get 'admins' => 'users#admins_dashboard'
delete 'users' => 'users#destroy'
get '/users/edit/:id' => 'users#edit'
resources :users

get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
end


My controller action for users:

def show
@user = User.find(params[:id])
end

def create
@user = User.new(user_params)
if @user.save
session[:user_id] = @user.id
redirect_to '/'
else
redirect_to '/signup'
end
end


the model for users:

class User < ActiveRecord::Base
has_secure_password

def admin?
self.role == 'admin'
end
end

Answer

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.

You had <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 http://example.com/logo.png.

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. image_tag("logo.png").