【问题标题】:Some help figuring out this ruby on rails favicon related error showing up in logs一些帮助找出这个 ruby​​ on rails favicon 相关错误出现在日志中
【发布时间】:2012-01-10 12:16:57
【问题描述】:

该网站运行良好,但我注意到此错误出现在我的日志中。

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)

当我调用我的个人资料表时似乎会发生这种情况:

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'

用户控制器:

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

显示视图:

<%= @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 %>

路线:

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'

不太清楚它为什么对网站图标感兴趣。

任何建议表示赞赏。

亲切的问候

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.1 rubygems


    【解决方案1】:

    看起来你有一个匹配 /:username 指向你的用户控制器的包罗万象的路由。因此,当有人点击 /favicon.ico 时,它会转到控制器,但找不到具有该用户名的用户。

    首先,如果您找不到用户名,您应该在您的用户控制器中发回 404。您可以轻松地添加一个 bang 来做到这一点:

    User.find_by_username!(params[:username])
    

    但是浏览器在那里寻找图标是很正常的(因为它是默认位置)。我会在路由文件中为网站图标添加一个路由和/或在您的 application.html.erb 中设置一个favicon_link_tag 以指向您的网站图标所在的位置。

    【讨论】:

    • 非常感谢。将 添加到 application.html.erb 中,然后将 favicon.ico 放入我的 assets/images 文件夹中。所有错误都消失了。谢谢
    【解决方案2】:
      match '/:username', :controller => 'users', :action => 'show'
    

    将对“/favicon.ico”的请求路由到 UsersController#show。

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

    尝试查找用户名为“favicon”的用户。它不能,所以它返回 nil。

     @profile = @user.profile
    

    然后尝试在返回的nil 对象上调用profile。这会导致您看到的错误。

    【讨论】:

    • 感谢您提供详细信息。前几天我以为这是正在发生的事情,但不知道如何阻止它。
    猜你喜欢
    • 2017-02-23
    • 1970-01-01
    • 2013-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多