# Overriding default Rails methods

**URL:** <https://www.ruby-forum.com/t/overriding-default-rails-methods/125574>\
**Category:** Rails\
**Created:** [January 10, 2008, 9:41am UTC](https://www.ruby-forum.com/t/overriding-default-rails-methods/125574 "2008-01-10T09:41:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Julian\_Frumar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@Julian\_Frumar](https://www.ruby-forum.com/u/Julian_Frumar)\
**Post date:** [January 10, 2008, 9:41am UTC](https://www.ruby-forum.com/t/overriding-default-rails-methods/125574/1 "2008-01-10T09:41:36Z")

</div>

Hey guys,

What’s the best practices for overriding default Rails methods? I can’t  
seem to find a guide on this topic anywhere online.

Here’s the deal - I want to override the request\_forgery\_protection for  
forms because we don’t set session\_ids for bots, and they are now  
generating exceptions.

I have created a file called ‘override\_request\_forgery\_protection.rb’,  
put it in /lib, and added to application.rb the line:

require ‘override\_request\_forgery\_protection.rb’

Now in the file I have put in the following code:

module ActionController::RequestForgeryProtection  
def self.included(base)  
base.class\_eval do  
alias\_method\_chain authenticity\_token\_from\_session\_id,  
:session\_check  
end  
base.extend(ClassMethods)  
end

module ClassMethods

```
def authenticity_token_from_session_id_with_session_check
  if defined? session.session_id # We have the session ID - behave

```

as rails normally would  
authenticity\_token\_from\_session\_id\_with\_session\_check  
else # We don’t have a session ID, instead of croaking, lets just  
say the authenticity token is bad  
return false  
end  
end

end

end

However, this code doesn’t seem to be being executed. I am setting  
breakpoints in eclipse, and they’re not being hit. Any ideas please?

Thanks!

---

<div class="post-metadata">

**Author:** ![Julian\_Frumar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@Julian\_Frumar](https://www.ruby-forum.com/u/Julian_Frumar)\
**Post date:** [January 10, 2008, 10:05am UTC](https://www.ruby-forum.com/t/overriding-default-rails-methods/125574/2 "2008-01-10T10:05:07Z")

</div>

I managed to get the desired outcome through the following (small)  
piece of code:

module ActionController  
module RequestForgeryProtection

```
def authenticity_token_from_session_id_with_session_check
  if defined? session.session_id # We have the session ID -

```

behave as rails normally would  
authenticity\_token\_from\_session\_id\_without\_session\_check  
else # We don’t have a session ID, instead of croaking, lets  
just say the authenticity token is bad  
return false  
end  
end

alias\_method\_chain :authenticity\_token\_from\_session\_id, :session\_check  
end

end
