简体   繁体   中英

SystemStackError - stack level too deep with a User Search

when searching for a User I end up getting a SystemStackError.

I've copied this over from another project I did which works fine, so I'm struggling to work out why it isn't working.

The suspect code:

class ProfilesController < ApplicationController
      before_filter :find_user

      def show    
        if @user
            render action: show
        else
            render file: 'public/404', status: 404, formats: [:html]
        end
      end

      private

      def find_user
        @user = User.find_by_profile_name(params[:id])
      end
    end

And the error:

Started GET "/Benji" for 127.0.0.1 at 2013-12-07 18:23:57 +0000
  ActiveRecord::SchemaMigration Load (0.5ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Processing by ProfilesController#show as HTML
  Parameters: {"id"=>"Benji"}
  User Load (0.9ms)  SELECT "users".* FROM "users" WHERE "users"."profile_name" = 'Benji' LIMIT 1
Completed 500 Internal Server Error in 111ms

SystemStackError - stack level too deep:
  actionpack (4.0.2) lib/action_dispatch/middleware/reloader.rb:70:in `'

Thanks

It should be

render action: "show"

not

render action: show

In your code, show is a method call and it references itself causing a recursive infinite loop.

You can simplify your code even more

class ProfilesController < ApplicationController
  before_filter :find_user

  def show
  end

  private

  def find_user
    unless @user = User.find_by_profile_name(params[:id])
      render file: 'public/404', status: 404, formats: [:html]
    end
  end
end

I believe the render issue definitely answers the question.

Also worth noting - in your find_user before filter, the Rails find_by_* method you are calling is searching via the profile name attribute of your model. However, you are passing in params[:id] to the method. Just wanted to make sure that was correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM