On Aug 15, 1:52 pm, "[email protected]" <[email protected]> wrote:
> So i've been banging my head against this for the better part of the
> last 5 hours and I have concluded that in my sleep deprived state I'm
> missing something really obvious and should just ask for help.
>
> I need to be able to access named route helpers (XXX_path and XXX_URL)
> form a class method in an ActiveRecord model. I tried a couple of
> approaches none of which worked:
>
> 1) include ActionController::UrlWriter in the model class -- this
> doesn't work because the included methods are instance methods and
> thus are not available in the class methods
>
> 2) According to the rails api you can access the helpers by using
> ActionController::UrlWriter.xxx_path (http://api.rubyonrails.org/
> classes/ActionController/UrlWriter.html). However that just doesn't
> work for me, I get the following error
>
> NoMethodError: undefined method `xxx_path' for
> ActionController::UrlWriter:Module
>
> If anyone has ANY ideas PLEASE let me know.

The helpers are defined in an anonymous module hidden inside the
routing stuff. You can ask rails to include that module in a class for
you, eg

class Foo
 include ActionController::UrlWriter
 def generate
  parent_categories_url :host => 'example.com'
 end
end
ActionController::Routing::Routes.named_routes.install(Foo)

Foo.new.generate #=> "http://example.com/admin/parent_categories";

Fred
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to