# Extend ActionController::UrlWriter results in NoMethodError:

**URL:** <https://www.ruby-forum.com/t/extend-actioncontroller-urlwriter-results-in-nomethoderror/179789>\
**Category:** Rails\
**Created:** [December 3, 2009, 8:18pm UTC](https://www.ruby-forum.com/t/extend-actioncontroller-urlwriter-results-in-nomethoderror/179789 "2009-12-03T20:18:11Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mperham](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47c2d/32.png) [@mperham](https://www.ruby-forum.com/u/mperham)\
**Post date:** [December 3, 2009, 8:18pm UTC](https://www.ruby-forum.com/t/extend-actioncontroller-urlwriter-results-in-nomethoderror/179789/1 "2009-12-03T20:18:11Z")

</div>

I am using extend within a class to use  
ActionController::UrlWriter.url\_for outside of a controller. The  
intention is to add url\_for as a class method of my class (since its a  
class with only class methods).

When I do this and make the call to url\_for, I get this error:

NoMethodError: undefined method `default\_url\_options’ for Class:Class

Heres some code:

class OutsideController  
extend ActionController::UrlWriter # include url\_for as a class method  
of OutsideController

def self.doit  
url = url\_for({:controller =\> ‘controller’, :action =\> “action”})  
end  
end

--------------url\_for method in UrlWriter module:  
def url\_for(options)  
options = self.class.default\_url\_options.merge(options) # ERROR  
OCCURS HERE

```
  url = ''

  unless options.delete(:only_path)
    url << (options.delete(:protocol) || 'http')
    url << '://' unless url.match("://")

    raise "Missing host to link to! Please provide :host parameter

```

or set default\_url\_options[:host]" unless options[:host]

```
    url << options.delete(:host)
    url << ":#{options.delete(:port)}" if options.key?(:port)
  else
    # Delete the unused options to prevent their appearance in the

```

query string.  
[:protocol, :host, :port, :skip\_relative\_url\_root].each { |k|  
options.delete(k) }  
end  
trailing\_slash = options.delete(:trailing\_slash) if  
options.key?(:trailing\_slash)  
url \<\< ActionController::Base.relative\_url\_root.to\_s unless  
options[:skip\_relative\_url\_root]  
anchor = “##{CGI.escape options.delete(:anchor).to\_param.to\_s}” if  
options[:anchor]  
generated = Routing::Routes.generate(options, {})  
url \<\< (trailing\_slash ? generated.sub(/?|\z/) { “/” + $& } :  
generated)  
url \<\< anchor if anchor

```
  url
end

```

* * *

I have tried several things to avoid the error, including also  
include/extending extra modules and setting:

extend ActionView::Helpers::UrlHelper  
default\_url\_options[:host] = ‘[www.example.com](http://www.example.com)’

Obviously I do not understand extend and the inner working of UrlWriter  
enough to do this correctly - can anyone help me out?

Mike
