500 when using url_for helper in controllers initialize

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" - Automotive CRM

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" - 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.

thanks

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

RSL