e3matheus
e3matheus

Reputation: 2132

Rails Engine isn't being loaded in asset precompile

I have a Rails Mountable Engine called Blog.

Inside the module, I have a method called root_path. The module loads the root path of the engine.

module Blog
  def self.root_path
    Engine.routes.url_helpers.root_path
  end
end

Inside one of the javascript assets of the Rails engine, I load the root url of the engine using erb syntax. Like the following line:

url = <%= Blog.root_path %>

When I run, rake assets:precompile, inside my app, I get an error saying the module does not contain such method. Like it isn't loading the engine library before precompiling the assets.

The error is:

undefined method `root_path' for #< Module:0xc185e14>

Upvotes: 1

Views: 956

Answers (2)

e3matheus
e3matheus

Reputation: 2132

Even though Ryan's answer was helpfull, it wasn't the reason I was getting the error.

The reason was that I had set sep up initialize_on_precompile to false in my config/application.rb, so my app wasn't being started.

The Rails guides clearly states:

*For faster asset precompiles, you can partially load your application by setting config.assets.initialize_on_precompile to false in config/application.rb, though in that case templates cannot see application objects or methods*

Upvotes: 2

Ryan Bigg
Ryan Bigg

Reputation: 107738

Rails engines provide their routing helpers through a routing proxy. You don't need to define root_path methods like this.

Instead, call the method which is your engine's name and then the routing helper on it like this:

blog.root_path

For more information, read the Engines Guide.

Upvotes: 2

Related Questions