简体   繁体   English

SystemStackError-用户搜索的堆栈级别太深

[英]SystemStackError - stack level too deep with a User Search

when searching for a User I end up getting a SystemStackError. 当搜索用户时,我最终遇到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. 在您的代码中, show是一个方法调用,它引用自身,从而导致递归无限循环。

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. 我相信render问题一定会回答这个问题。

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. 还值得注意的是-在过滤器之前的find_user ,您正在调用的Rails find_by_*方法是通过模型的配置文件名称属性进行搜索的。 However, you are passing in params[:id] to the method. 但是,您正在将params[:id]传递给该方法。 Just wanted to make sure that was correct. 只是想确保这是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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