Question

The site is working fine but I notice this error showing up in my logs.

Started GET "/favicon.ico" for 127.0.0.1 at 2012-01-10 12:03:54 +0000
  Processing by UsersController#show as 
  Parameters: {"username"=>"favicon"}
  User Load (0.1ms)  SELECT "users".* FROM "users" WHERE "users"."username" = 'favicon' LIMIT 1
Completed 500 Internal Server Error in 1ms

NoMethodError (undefined method `profile' for nil:NilClass):
  app/controllers/users_controller.rb:32:in `show'

Rendered /Users/greg/.rvm/gems/ruby-1.9.3-p0@devvo/gems/actionpack-3.1.1/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.3ms)
Rendered /Users/greg/.rvm/gems/ruby-1.9.3-p0@devvo/gems/actionpack-3.1.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
Rendered /Users/greg/.rvm/gems/ruby-1.9.3-p0@devvo/gems/actionpack-3.1.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (5.0ms)

It seems to happen when ever calls to my profiles table are made:

Started GET "/favicon.ico" for 127.0.0.1 at 2012-01-10 12:14:10 +0000
  Processing by UsersController#show as 
  Parameters: {"username"=>"favicon"}
  User Load (0.1ms)  SELECT "users".* FROM "users" WHERE "users"."username" = 'favicon' LIMIT 1
Completed 500 Internal Server Error in 1ms

NoMethodError (undefined method `profile' for nil:NilClass):
  app/controllers/users_controller.rb:32:in `show'

Users controller:

class UsersController < ApplicationController

  before_filter :correct_user, :only => [:edit, :update]
  before_filter :logged_in_now, :only => [:settings]


  def new
    @user = User.new 
    @title = "Practice"
  end

  def create
    @user = User.new(params[:user])   
    respond_to do |format|
      if @user.save 
        @user.build_profile.save #same as Profile.new(:user_id => @user.id)
        login @user
        UserMailer.join_confirmation(@user).deliver
        format.js   { render :js => "window.location = '#{root_path}'" } 
     #   flash[:notice] = "Welcome!"
      else

        format.js   { render :form_errors }
      end
    end
  end


  def show

     @user = User.find_by_username(params[:username])
     @profile = @user.profile

  end

  def update
     respond_to do |format|
   @user = User.find(params[:id])
    if @user.update_attributes(params[:user])
    login @user
    format.js   { render :js => "window.location = '#{settings_account_path}'" } 
    flash[:success] = "Profile updated" 
    else
     format.js   { render :form_errors }

      end
      end
  end

  def edit

  end

  def settings
  @user = current_user
  #redirect_to settings_account_path
  end

  def account
  @user = current_user
  end

  private

  # def authenticate
  #   deny_access unless logged_in?
  # end

  def correct_user
    @user = User.find(params[:id])
    redirect_to root_path unless current_user?(@user)
  end

     def logged_in_now

    redirect_to root_path if !logged_in?

  end


end

Show view:

<%= @profile.motd %> <br />
<%= @profile.first_name %> <br />
<%= @profile.last_name %> <br />
<%= @profile.user_id %> <br />
<%= @user.id %> <br />
<%= @user.username %> <br />
<%= @user.email %><br />
Star sign: <%= @profile.birthday.zodiac_sign %>

Routes:

Swoggo::Application.routes.draw do

  resources :users
  resources :sessions
  resources :passwords
  resources :profiles


  root :to                   => "users#new"
  match 'success'            => "users#success"
  match 'login'              => "sessions#new"
  match 'logout'             => "sessions#destroy"
  match 'reset_password'     => "passwords#new"
  match 'setup_new_password' => "passwords#edit"
  match 'settings', :to      => "users#settings"


  match "/settings/account", :to => "users#account"
  match "/settings/edit_profile", :to => "profiles#edit_profile"


  match '/:username', :controller => 'users', :action => 'show'

Not quite sure why it's interested in the favicon.

Any advice appreciated.

Kind regards

Was it helpful?

Solution

It looks like you have a catch-all route that matches /:username that points to your users controller. So when someone hits /favicon.ico it goes to the controller but can't find the user with that username.

First, in your users controller you should be sending back a 404 if you can't find a username. You can easily add a bang to do this:

User.find_by_username!(params[:username])

But it's pretty normal for browsers to look for the favicon there (because it's the default location). I would either add a route for the favicon in routes file and/or set a favicon_link_tag in your application.html.erb to point where your favicon is.

OTHER TIPS

  match '/:username', :controller => 'users', :action => 'show'

routes requests for "/favicon.ico" to UsersController#show.

 @user = User.find_by_username(params[:username])

tries to find a User with username 'favicon'. It can't, so it returns nil.

 @profile = @user.profile

then tries to call profile on the returned nil object. That results in the error you see.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top