21

I couldn't find this anywhere and just went in circles with it so I figured someone else might benefit. How do you get access to rails url helpers in an engine?

For the core app, I can do something like this:

class Thingy < ActiveRecord::Base    
 include Rails.application.routes.url_helpers
 ...
end

But this doesnt work in models in an engine.

Pcushing
  • 321
  • 2
  • 7
  • 1
    Apparently I can't answer my own question yet as a SO noob so here's the answer in a comment for now: Looks like you need to specify the routes that're particular to your engine. So in your engine's model, for example, you can do this: module Blog class Stuffy include Blog::Engine.routes.url_helpers ... end end And now you can use the url helpers from your engine inside your engine's models. – Pcushing Feb 04 '14 at 23:06
  • possible duplicate of [Can Rails Routing Helpers (i.e. mymodel\_path(model)) be Used in Models?](http://stackoverflow.com/questions/341143/can-rails-routing-helpers-i-e-mymodel-pathmodel-be-used-in-models) – Baldrick May 02 '14 at 16:16

1 Answers1

10

Looks like you need to specify the routes that're particular to your engine. So in your engine's model, for example, you can do this:

module Blog
 class Stuffy
  include Blog::Engine.routes.url_helpers
  ...
 end
end

And now you can use the url helpers from your engine inside your engine's models.

Pcushing
  • 321
  • 2
  • 7
  • As your mentioned in your question- someone else might benefit - looks like I did. Thanks a lot for this one!!! Spent half a day looking for a work around. – Alex Jose Apr 20 '16 at 09:18