rails 3.1.1 engines - with mountable engines, is it possible to access parent app assets, default layout?

北战南征 提交于 2019-12-03 14:53:18

I managed to get this working with the following steps:

  1. In my parent app I was mounting the engine in routes.rb

    mount PluginName::Engine => '/plugin_name'
    

    I just removed it.

  2. Created an application controller as Ryan Bigg below had stated.

    class PluginName::ApplicationController < ApplicationController
      ...
    end
    
  3. As I wanted to have things name spaced when generating controllers, models, tests so you have to essentially comment out the isolate_namespace PluginName lib\plugin_name\engine.rb when I wanted the gem to be run in the parent app.

    It is not yet an ideal solution. off the top off my head, I could use something like:

    isolate_namespace PluginName if %w[development testing].include?(Rails.env)
    

    but will have to test if this is practical.

Kudos to Ryan for helping me find my way many thanks

Furthermore, the same can be done with the --mountable switch version and all you need to do is one further step in your engines config/routes.rb replace

PluginName::Engine.routes.draw do

with

Rails.application.routes.draw do

Yes, you can reference the parent application assets just by referencing them in your application like normal:

  <%= stylesheet_link_tag "application %>

Although, not sure why you would want to do that because...

I'm going to answer your first question with the answer to your second question.

To use the application's layout you will need to modify the ApplicationController in the engine (which is namespaced) and have it inherit from ApplicationController in the engine.

That will then have the controllers for the engine using the layout provided by the engine. I'm doing this in my engine, forem.

One day, this will be covered in the Engines Guide that, at this time of writing, is currently being written.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!