Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using url_for in a lib folder module

I have a model with a field called "type" and a field called "value". The type field will determine how the "value" is parsed before rendering. I want this to be easily extendible, so I'm putting a variety of "formatter" classes in my lib folder.

One of my "formatters" has a call to url_for:

class CustomTypeFormatter 
  include ActionView::Helpers::TextHelper
  include ActionView::Helpers

  def show
    raw sanitize( auto_link( value ) )
  end

  def get_url(page)
    url_for( :controller => :my_controller, :action => :show, :path => page.path )
  end

end

The problem is, url_for is raising this error:

undefined local variable or method `_routes'

I think I'm just missing an include. Does anyone have an idea what it should be?

**UPDATE***

Here's part of the stack trace:

actionpack (3.0.7) lib/action_dispatch/routing/url_for.rb:131:in `url_for'
actionpack (3.0.7) lib/action_view/helpers/url_helper.rb:99:in `url_for'

So I think it must be some ActiveDispatch dependency, although I cannot figure out

like image 622
earnold Avatar asked May 03 '11 20:05

earnold


1 Answers

Victory!

include ActionView::Helpers
include ActionDispatch::Routing
include Rails.application.routes.url_helpers

via http://apidock.com/rails/ActionController/UrlWriter

like image 144
Peter Ehrlich Avatar answered Sep 23 '22 00:09

Peter Ehrlich