# Unsure How To Modify ActionController::Base

**URL:** <https://www.ruby-forum.com/t/unsure-how-to-modify-actioncontroller-base/76184>\
**Category:** Rails\
**Created:** [November 16, 2006, 3:48pm UTC](https://www.ruby-forum.com/t/unsure-how-to-modify-actioncontroller-base/76184 "2006-11-16T15:48:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [November 16, 2006, 3:48pm UTC](https://www.ruby-forum.com/t/unsure-how-to-modify-actioncontroller-base/76184/1 "2006-11-16T15:48:06Z")

</div>

Hello,

I would like to add a couple of methods like url\_for in such a way  
that I can call these other methods in my actions just as I might  
call url\_for. However I keep getting undefined method errors when I  
execute those calls.

Here’s what I did:

1. Generated a plugin: $ script/generate plugin UrlForRouter

2. Created the file RAILS\_ROOT/vendor/plugins/url\_for\_router/lib/  
url\_for\_router.rb  
This file looks like:

(:host =\> host)) : url\_for(options)  
end  
end

1. Edited RAILS\_ROOT/vendor/plugins/url\_for\_router/init.rb to look  
like this:

2. Restarted web server.

3. In my actions I call internal\_url\_for and external\_url\_for  
exactly as I would call url\_for.  
I get the error: undefined method `external\_url\_for’ for …etc

Is this even the right way to proceed? If so, what am I doing wrong?

Many thanks,  
Andy S.

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [November 16, 2006, 5:00pm UTC](https://www.ruby-forum.com/t/unsure-how-to-modify-actioncontroller-base/76184/2 "2006-11-16T17:00:32Z")

</div>

Andrew S. wrote:

> ```
> host_url_for('xxx.xxx.xxx.xxx', options) # anonymised
> 
> ```
> 
> like this:  
> Is this even the right way to proceed? If so, what am I doing wrong?
> 
> Many thanks,  
> Andy S.
> 
> >

Andrew,

I think that include method is private; not 100% sure, but I recall  
having similiar troubles like you described. So you would have to  
include it like:

ActionController::Base.send :include, UrlForRouter

–  
Bojan M.  
Informatika Mihelac, Bojan M. s.p. | [www.informatikamihelac.com](http://www.informatikamihelac.com)  
 → tools, scripts, tricks from our code lab: [http://source.mihelac.org](http://source.mihelac.org)

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [November 16, 2006, 5:02pm UTC](https://www.ruby-forum.com/t/unsure-how-to-modify-actioncontroller-base/76184/3 "2006-11-16T17:02:33Z")

</div>

On 11/16/06, Andrew S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 1. Generated a plugin: $ script/generate plugin UrlForRouter  
> def external\_url\_for(options = {})
> 2. Edited RAILS\_ROOT/vendor/plugins/url\_for\_router/init.rb to look
> 
> Is this even the right way to proceed? If so, what am I doing wrong?
> 
> Many thanks,  
> Andy S.
> 
> >

Andy,

Unless you want to share these methods across multiple apps don’t  
bother making a plugin. Just do the following:

1. Put url\_for\_router.rb in your lib/
2. Include the module in application.rb

class ApplicationController \< ActionController::Base  
include UrlForRouter  
…  
end

1. That’s it!

Also, if you want to use these methods in your views you can simply  
add the following to your module.

module UrlForRouter

```
# --- [ActionView helper methods] ---
def self.included(base)
  base.send :helper_method, :internal_url_for, :external_url_for
end

```

end

Hope this helps.

–  
Zack C.  
[http://depixelate.com](http://depixelate.com)

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [November 16, 2006, 6:48pm UTC](https://www.ruby-forum.com/t/unsure-how-to-modify-actioncontroller-base/76184/4 "2006-11-16T18:48:54Z")

</div>

On 16 Nov 2006, at 15:46, Bojan M. wrote:

> I think that include method is private; not 100% sure, but I recall  
> having similiar troubles like you described. So you would have to  
> include it like:
> 
> ActionController::Base.send :include, UrlForRouter

Bojan, thanks for this. I tried it but unfortunately it didn’t work  
for me. However that’s probably down to some other flaw on my part  
rather than your suggestion.

Thanks,  
Andy

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [November 16, 2006, 6:46pm UTC](https://www.ruby-forum.com/t/unsure-how-to-modify-actioncontroller-base/76184/5 "2006-11-16T18:46:53Z")

</div>

On 16 Nov 2006, at 15:57, Zack C. wrote:

> Unless you want to share these methods across multiple apps don’t  
> bother making a plugin. Just do the following:

I was thinking that I would reuse this code in future apps. Now that  
you mention it though, there’s no need at the moment. So I’m going  
with your approach.

> 1. Put url\_for\_router.rb in your lib/
> 2. Include the module in application.rb
> 
> class ApplicationController \< ActionController::Base  
> include UrlForRouter  
> …  
> end
> 
> 1. That’s it!

That works wonderfully in my controller. Thanks!

However I also have a model (which doesn’t subclass ActiveRecord)  
where I am trying to call my {internal, external}\_url\_for methods and  
I get this error:

undefined method `url_for' #{RAILS_ROOT}/lib/url_for_router.rb:16:in`host\_url\_for’  
#{RAILS\_ROOT}/lib/url\_for\_router.rb:10:in `external\_url\_for’

My model class finds and calls my methods but the UrlForRouter module  
can’t resolve url\_for in this context. What should I do here?

> end  
> Very nice!

> Hope this helps.

Yes it does, very much.

Thanks!  
Andy

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [November 17, 2006, 12:57pm UTC](https://www.ruby-forum.com/t/unsure-how-to-modify-actioncontroller-base/76184/6 "2006-11-17T12:57:49Z")

</div>

> 

Just for the record, I found this post[1] which argues persuasively  
for keeping knowledge of URLs out of models (as well as showing a  
good way to generate URLs within ActionMailer). So I moved the URL  
knowledge out of my model. Simple, really, when I thought about it.

Regards,  
Andy

[1] [http://www.ryandaigle.com/articles/2006/08/23/whats-new-in-edge-](http://www.ryandaigle.com/articles/2006/08/23/whats-new-in-edge-)  
rails-form-urls-in-your-actionmailers-and-elsewhere
