# Using a plugin to modify Rails core?

**URL:** <https://www.ruby-forum.com/t/using-a-plugin-to-modify-rails-core/48558>\
**Category:** Rails\
**Created:** [November 22, 2005, 3:33am UTC](https://www.ruby-forum.com/t/using-a-plugin-to-modify-rails-core/48558 "2005-11-22T03:33:02Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![petermichaux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/46a35a/32.png) [@petermichaux](https://www.ruby-forum.com/u/petermichaux)\
**Post date:** [November 22, 2005, 3:33am UTC](https://www.ruby-forum.com/t/using-a-plugin-to-modify-rails-core/48558/1 "2005-11-22T03:33:02Z")

</div>

Hi,

I’ve been trying to learn about plugins by reading plugin source files  
where  
I can find them. I feel like the fog is clearing a little. Is it true  
that a  
plugin can be used to modify any part of the Rails core? If so, how can  
I do  
it? For example, say I want to create a plugin in  
vendor/plugin/new\_error  
parts of ActiveRecord’s Errors class.

Trimmed down from ActiveRecord it has this:

module ActiveRecord  
class Errors  
@@default\_error\_messages = {  
:inclusion =\> “is not included in the list”,  
:exclusion =\> “is reserved”,  
:invalid =\> “is invalid”  
}

def add\_to\_base(msg)  
add(:base, msg)  
end  
end  
end

and say I want to change it to this (I know, silly example):

module ActiveRecord  
class Errors  
@@default\_error\_messages = {  
:inclusion =\> “is not included in the list, idiot”,  
:exclusion =\> “is reserved, ignoramus”  
}

def add\_to\_base(msg)  
foo = msg  
add(:base, foo)  
end  
end  
end

Would I need to have a vendor/plugin/new\_error/init.rb file? What would  
it  
say?

Would vendor/plugin/new\_error/lib/new\_error.rb simply be what I want  
above?

Thanks,  
Peter

---

<div class="post-metadata">

**Author:** ![petermichaux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/46a35a/32.png) [@petermichaux](https://www.ruby-forum.com/u/petermichaux)\
**Post date:** [November 22, 2005, 7:14am UTC](https://www.ruby-forum.com/t/using-a-plugin-to-modify-rails-core/48558/2 "2005-11-22T07:14:13Z")

</div>

hey everyone,  
i read this article:  
[http://www.bitslapped.nl/articles/2005/06/05/rails-on-windows-2003](http://www.bitslapped.nl/articles/2005/06/05/rails-on-windows-2003)  
but am still unable to get RoR working on win2k3/iis6. i can confirm  
that my app is working with WEBrick, but whenever i try to access it  
with IIS, i get a 404 error. i am pretty sure i have everything  
configured correctly.  
anyone have any tips to get this running?  
thanks a lot!  
steve

---

<div class="post-metadata">

**Author:** ![petermichaux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/46a35a/32.png) [@petermichaux](https://www.ruby-forum.com/u/petermichaux)\
**Post date:** [November 22, 2005, 10:59am UTC](https://www.ruby-forum.com/t/using-a-plugin-to-modify-rails-core/48558/3 "2005-11-22T10:59:44Z")

</div>

In your init.rb file, you should require your new\_error.rb file, and  
that should be all you need. I.e.

vendor/plugins/new\_error/init.rb:

require ‘new\_error’ # note no ‘lib/’ here

vendor/plugins/new\_error/lib/new\_error.rb

module ActiveRecord  
class Errors  
@@default\_error\_messages = {  
:inclusion =\> “is not included in the list, idiot”,  
:exclusion =\> “is reserved, ignoramus”  
}

```
   def add_to_base(msg)
      foo = msg
      add(:base, foo)
  end
end

```

end

… and that should do the trick. Plugin code is (almost) the last  
thing to load, so you can quite easily use it to hack about with Rails  
internals (as I do to a small degree with the Engines plugin).

Hope this helps

- James

---

<div class="post-metadata">

**Author:** ![petermichaux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/46a35a/32.png) [@petermichaux](https://www.ruby-forum.com/u/petermichaux)\
**Post date:** [November 22, 2005, 2:57pm UTC](https://www.ruby-forum.com/t/using-a-plugin-to-modify-rails-core/48558/4 "2005-11-22T14:57:24Z")

</div>

Quick tip, delete all the log files in your app and restart IIS (not  
just the site your app is using, the whole thing). Do you get log  
files generated again? If so you’re headed in the right direction. You  
should have the environmnet log, and a couple fast-cgi logs generated.

Let us know if you get these or not and we can go from there. 🙂

-Nick

---

<div class="post-metadata">

**Author:** ![petermichaux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/46a35a/32.png) [@petermichaux](https://www.ruby-forum.com/u/petermichaux)\
**Post date:** [November 22, 2005, 3:57pm UTC](https://www.ruby-forum.com/t/using-a-plugin-to-modify-rails-core/48558/5 "2005-11-22T15:57:46Z")

</div>

ok, did that. now i’m getting “Server Error, unable to connect to  
fastcgi server.”  
and no logs have been created in the log dir.  
thanks for the tip…i guess i messed something up along the way while  
configuring fastCGI?

---

<div class="post-metadata">

**Author:** ![petermichaux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/46a35a/32.png) [@petermichaux](https://www.ruby-forum.com/u/petermichaux)\
**Post date:** [November 23, 2005, 2:26am UTC](https://www.ruby-forum.com/t/using-a-plugin-to-modify-rails-core/48558/6 "2005-11-23T02:26:14Z")

</div>

I’m having troubles getting this to work. I have the following files and  
reboot WEBrick but I do not see my redefined error messages. Any ideas?

Thanks,  
Peter

= vendor/plugins/new\_error/init.rb: =

require ‘new\_error’ # note no ‘lib/’ or ‘.rb’ here

= vendor/plugins/new\_error/lib/new\_error.rb =

module ActiveRecord  
class Errors  
@@default\_error\_messages = {  
:inclusion =\> “pm is not included in the list”,  
:exclusion =\> “pm is reserved”,  
:invalid =\> “pm is invalid”,  
:confirmation =\> “pm doesn’t match confirmation”,  
:accepted =\> “pm must be accepted”,  
:empty =\> “pm can’t be empty”,  
:blank =\> “pm can’t be blank”,  
:too\_long =\> “pm is too long (max is %d characters)”,  
:too\_short =\> “pm is too short (min is %d characters)”,  
:wrong\_length =\> “pm is the wrong length (should be %d  
characters)”,  
:taken =\> “pm has already been taken”,  
:not\_a\_number =\> “pm is not a number”  
}  
end  
end

---

<div class="post-metadata">

**Author:** ![petermichaux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/46a35a/32.png) [@petermichaux](https://www.ruby-forum.com/u/petermichaux)\
**Post date:** [November 23, 2005, 3:10am UTC](https://www.ruby-forum.com/t/using-a-plugin-to-modify-rails-core/48558/7 "2005-11-23T03:10:28Z")

</div>

It works just fine. Even though I am using Rails 14.3 the project was  
generated with a Rails version less than 0.14 so the plugins didn’t  
work. I  
didn’t know the Rails version generating the app mattered. I genrated a  
new  
app and tried again and everything worked as expected.

-Peter

---

<div class="post-metadata">

**Author:** ![petermichaux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/46a35a/32.png) [@petermichaux](https://www.ruby-forum.com/u/petermichaux)\
**Post date:** [November 23, 2005, 2:34am UTC](https://www.ruby-forum.com/t/using-a-plugin-to-modify-rails-core/48558/8 "2005-11-23T02:34:16Z")

</div>

Do I have to make a reference to the plugin somewhere in my app to make  
sure  
that the plugin loads? I thought plugins were supposed to be auto  
loading.

(If I put the vendor/plugins/new\_error/lib/new\_error.rb code in  
application\_controller.rb then I do see the new error messages. So the  
plugin is not loading.)

Thanks,  
Peter

---

<div class="post-metadata">

**Author:** ![petermichaux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/46a35a/32.png) [@petermichaux](https://www.ruby-forum.com/u/petermichaux)\
**Post date:** [November 23, 2005, 1:52pm UTC](https://www.ruby-forum.com/t/using-a-plugin-to-modify-rails-core/48558/9 "2005-11-23T13:52:36Z")

</div>

You can also updgrade existing apps. From your current apps directory  
just  
run `rails .` and it will go through and update stuff. Becareful with  
this  
though, as it will override most of your config files.
