# 500 when using url\_for helper in controllers initialize

**URL:** <https://www.ruby-forum.com/t/500-when-using-url-for-helper-in-controllers-initialize/88827>\
**Category:** Rails\
**Created:** [March 14, 2007, 7:57pm UTC](https://www.ruby-forum.com/t/500-when-using-url-for-helper-in-controllers-initialize/88827 "2007-03-14T19:57:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![maunzinha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/839c29/32.png) [@maunzinha](https://www.ruby-forum.com/u/maunzinha)\
**Post date:** [March 14, 2007, 7:57pm UTC](https://www.ruby-forum.com/t/500-when-using-url-for-helper-in-controllers-initialize/88827/1 "2007-03-14T19:57:57Z")

</div>

It seems like you can’t use the url\_for helper in the initialize  
method of a controller:

class TestController \< ApplicationController  
def initialize  
@somevar = url\_for ( :action =\> ‘index’ ) #\<=== This errors out  
for some reason  
end

def some\_method  
@somevar = url\_for ( :action =\> ‘index’ ) #\<=== but this works  
end  
end

Any idea why this is happening? Is there a known work around?

Thanks

Rafael

–  
[http://www.bdcsoftware.com](http://www.bdcsoftware.com)" - Automotive CRM

---

<div class="post-metadata">

**Author:** ![maunzinha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/839c29/32.png) [@maunzinha](https://www.ruby-forum.com/u/maunzinha)\
**Post date:** [March 14, 2007, 8:16pm UTC](https://www.ruby-forum.com/t/500-when-using-url-for-helper-in-controllers-initialize/88827/2 "2007-03-14T20:16:36Z")

</div>

Rafael S. wrote:

> It seems like you can’t use the url\_for helper in the initialize  
> method of a controller:
> 
> class TestController \< ApplicationController  
> def initialize  
> @somevar = url\_for ( :action =\> ‘index’ ) #\<=== This errors out  
> for some reason  
> end
> 
> def some\_method  
> @somevar = url\_for ( :action =\> ‘index’ ) #\<=== but this works  
> end  
> end
> 
> Any idea why this is happening? Is there a known work around?
> 
> Thanks
> 
> Rafael
> 
> –  
> [http://www.bdcsoftware.com](http://www.bdcsoftware.com)" - Automotive CRM

Rather than initialize, try a before\_filter

class TestController \< ApplicationController  
before\_filter :set\_some\_var

```
def index
  #renders index.rhtml
end

private
  def set_some_var
     @somevar = url_for ( :action => 'index' )
  end

```

end

Now you can use @somevar in every action that goes through your  
controller.

---

<div class="post-metadata">

**Author:** ![maunzinha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/839c29/32.png) [@maunzinha](https://www.ruby-forum.com/u/maunzinha)\
**Post date:** [March 14, 2007, 11:29pm UTC](https://www.ruby-forum.com/t/500-when-using-url-for-helper-in-controllers-initialize/88827/3 "2007-03-14T23:29:34Z")

</div>

thanks

---

<div class="post-metadata">

**Author:** ![maunzinha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/839c29/32.png) [@maunzinha](https://www.ruby-forum.com/u/maunzinha)\
**Post date:** [March 15, 2007, 4:28pm UTC](https://www.ruby-forum.com/t/500-when-using-url-for-helper-in-controllers-initialize/88827/4 "2007-03-15T16:28:42Z")

</div>

If you use/d named routes you can/could just use index\_url instead of  
having  
to assign a variable.

RSL
