views:

38

answers:

1

I need to generate a “report” in a model. This “report” has to include links. However, all the *_path functions seems to be unavailable in the model classes (a simple test show me that new_post_path raises an Exception in my models, but works fine in my controllers)

Is there any way to use polymorphic_url or, at least, *_path methods in my models ?

A: 

if you:

include ActionController::PolymorphicRoutes

In your model:

class SomeModel < ActiveRecord::Base
  include ActionController::PolymorphicRoutes
end

you get polymorphic_path and polymorphic_url. These can then be used by passing self into the methods if you're generating a route for the current AR object.

brad
Doesn't work ; Rails.logger.debug polymorphic_url(Post.last) give me undefined method `post_url'
Hwold
hmm... must require some other modules in there to create post_url. Try looking at the docs for ActionController::PolymorphicRoutes: http://api.rubyonrails.org/classes/ActionController/PolymorphicRoutes.html
brad
Seems like *_url and *_path are protected instance methods of ActionController::Base. Looks like I’ll have to find another way.
Hwold