# Stack level too deep

**URL:** <https://www.ruby-forum.com/t/stack-level-too-deep/105798>\
**Category:** Rails\
**Created:** [June 3, 2007, 12:00am UTC](https://www.ruby-forum.com/t/stack-level-too-deep/105798 "2007-06-03T00:00:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hamid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5fc32e/32.png) [@hamid](https://www.ruby-forum.com/u/hamid)\
**Post date:** [June 3, 2007, 12:00am UTC](https://www.ruby-forum.com/t/stack-level-too-deep/105798/1 "2007-06-03T00:00:43Z")

</div>

Hello,

I am new in using Rails.  
I want to implement a login system.  
I got this error: stack level too deep

I have following methods:  
signup  
login

thanks a lot for your help

---

<div class="post-metadata">

**Author:** ![hamid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5fc32e/32.png) [@hamid](https://www.ruby-forum.com/u/hamid)\
**Post date:** [June 3, 2007, 12:02am UTC](https://www.ruby-forum.com/t/stack-level-too-deep/105798/2 "2007-06-03T00:02:57Z")

</div>

It’s hard to tell without looking at your code, but I’m guessing that  
you’re getting some sort of recursive function call; make sure that your  
before\_filter is calling what it’s supposed to.

Hamid wrote:

> Hello,
> 
> I am new in using Rails.  
> I want to implement a login system.  
> I got this error: stack level too deep
> 
> I have following methods:  
> signup  
> login
> 
> thanks a lot for your help

---

<div class="post-metadata">

**Author:** ![hamid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5fc32e/32.png) [@hamid](https://www.ruby-forum.com/u/hamid)\
**Post date:** [June 3, 2007, 12:07am UTC](https://www.ruby-forum.com/t/stack-level-too-deep/105798/3 "2007-06-03T00:07:43Z")

</div>

jimtron wrote:

> It’s hard to tell without looking at your code, but I’m guessing that  
> you’re getting some sort of recursive function call; make sure that your  
> before\_filter is calling what it’s supposed to.
> 
> Hamid wrote:
> 
> > Hello,
> > 
> > I am new in using Rails.  
> > I want to implement a login system.  
> > I got this error: stack level too deep
> > 
> > I have following methods:  
> > signup  
> > login
> > 
> > thanks a lot for your help

Hello  
this is my code:  
thank you

class SecuredController \< ApplicationController  
before\_filter :authorize, :except =\> :login  
def login  
case @request.method  
when :post  
if @session[:person] = Person.authenticate( @params[:person\_name],  
params[:person\_password])  
flash[‘notice’] = “Anmeldung erfolgreich”  
#redirect\_back\_or\_default :action =\> :signup  
#redirect\_to :action =\> “welcome”  
@name = @params[:person\_name]  
end  
end  
end

def signup  
@person = Person.new(@params[:person])

```
if @request.post? and @person.save
  @session[:person] = Person.authenticate( @person.name, 

```

@params[:person][:password])  
flash[‘notice’] = “Registrierung erfolgreich”  
redirect\_back\_or\_default :action =\> “welcome”  
#redirect\_to :action =\> “welcome”  
end  
end

def logout  
@session[:person] = nil  
end

def welcome  
end

end

---

<div class="post-metadata">

**Author:** ![hamid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5fc32e/32.png) [@hamid](https://www.ruby-forum.com/u/hamid)\
**Post date:** [June 3, 2007, 4:20am UTC](https://www.ruby-forum.com/t/stack-level-too-deep/105798/4 "2007-06-03T04:20:06Z")

</div>

def password=(pwd)  
@password = pwd  
return if pwd.blank?  
create\_new\_salt  
#self.hashed\_password = Person.encrypted\_password(self.password,  
self.salt)  
self.password = Person.encrypted\_password(self.password, self.salt)  
end

This method is calling itself on the last line. Right before the  
final “end.” It will continue to do this until it runs out of stack  
space and your program crashes.

On Jun 2, 6:15 pm, Abdelhamid N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![hamid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/5fc32e/32.png) [@hamid](https://www.ruby-forum.com/u/hamid)\
**Post date:** [June 3, 2007, 12:15am UTC](https://www.ruby-forum.com/t/stack-level-too-deep/105798/5 "2007-06-03T00:15:52Z")

</div>

Abdelhamid N. wrote:

> jimtron wrote:
> 
> > It’s hard to tell without looking at your code, but I’m guessing that  
> > you’re getting some sort of recursive function call; make sure that your  
> > before\_filter is calling what it’s supposed to.
> > 
> > Hamid wrote:
> > 
> > > Hello,
> > > 
> > > I am new in using Rails.  
> > > I want to implement a login system.  
> > > I got this error: stack level too deep
> > > 
> > > I have following methods:  
> > > signup  
> > > login
> > > 
> > > thanks a lot for your help
> 
> Hello  
> this is my code:  
> thank you
> 
> class SecuredController \< ApplicationController  
> before\_filter :authorize, :except =\> :login  
> def login  
> case @request.method  
> when :post  
> if @session[:person] = Person.authenticate( @params[:person\_name],  
> params[:person\_password])  
> flash[‘notice’] = “Anmeldung erfolgreich”  
> #redirect\_back\_or\_default :action =\> :signup  
> #redirect\_to :action =\> “welcome”  
> @name = @params[:person\_name]  
> end  
> end  
> end
> 
> def signup  
> @person = Person.new(@params[:person])
> 
> ```
> if @request.post? and @person.save
> @session[:person] = Person.authenticate( @person.name, 
> 
> ```
> 
> @params[:person][:password])  
> flash[‘notice’] = “Registrierung erfolgreich”  
> redirect\_back\_or\_default :action =\> “welcome”  
> #redirect\_to :action =\> “welcome”  
> end  
> end
> 
> def logout  
> @session[:person] = nil  
> end
> 
> def welcome  
> end
> 
> end

the model person it looks like:

require ‘digest/sha1’

class Person \< ActiveRecord::Base

validates\_presence\_of :name  
validates\_uniqueness\_of :name

attr\_accessor :password\_confirmation  
validates\_confirmation\_of :password

def validate  
errors.add\_to\_base(“Missing password”) if hashed\_password.blank?  
end

def self.authenticate(name, password)  
person = self.find\_by\_name(name)  
if person  
expected\_password = encrypted\_password(password, person.salt)  
if person.hashed\_password != expected\_password  
person = nil  
end  
end  
end

# 

# ‘password’ is a virtual attribute

def password  
@password  
end

def password=(pwd)  
@password = pwd  
return if pwd.blank?  
create\_new\_salt  
#self.hashed\_password = Person.encrypted\_password(self.password,  
self.salt)  
self.password = Person.encrypted\_password(self.password, self.salt)  
end

def create\_new\_salt  
self.salt = self.object\_id.to\_s + rand.to\_s  
end

private

def self.encrypted\_password(password, salt)  
string\_to\_hash = password + “wibble” + salt # ‘wibble’ makes it harder  
to guess  
Digest::SHA1.hexdigest(string\_to\_hash)  
end

@@salt = ‘[ioaruoagr]’  
cattr\_accessor :salt

end
