rails 3.1.1 引擎 - 使用可安装的引擎,是否可以访问父应用程序资产、默认布局? [英] rails 3.1.1 engines - with mountable engines, is it possible to access parent app assets, default layout?

查看:44
本文介绍了rails 3.1.1 引擎 - 使用可安装的引擎,是否可以访问父应用程序资产、默认布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这更多是为了实验 - 我知道我可以用 --full 来做到这一点,但我希望应用程序中的命名空间功能避免冲突

This is more for experimentation - I am aware that I can do this with --full but I wanted the functionality of namespacing in the app to avoid conflicts

这个想法是有一个主应用程序 - 处理身份验证、常用项目、管理屏幕等然后创建引擎以添加更多功能,例如

The idea is to have a main app - which handles authentication, common items, admin screens etc Then creating engines to add further functionality like

  • 客户关系管理
  • 厘米
  • 博客
  • 维基
  • 论坛

我可以根据需要为我正在构建的任何类型的应用程序挑选这些引擎.这可能吗?

These engines I can pick and choose as I need for whatever kind of app I am building. Is this possible?

是否只是同时应用 --mountable 和 --full 选项的情况?

Is it just the case of applying both --mountable and --full options?

实验中 - 如果我使用完整选项添加 rspec 然后简单添加会不会有任何问题

Experimenting - would there be any issue if I use the full option add rspec and then simple add

rails plugin new plugin_name --skip-test-unit --full --dummy-path=spec/dummy

并在 lib\plugin_name\engine.rb 中

and in lib\plugin_name\engine.rb

module PluginName
  class Engine < Rails::Engine

    # this is added by rails when an engine is mountable 
    # to isolate the plugin and prevent name clashes
    isolate_namespace PluginName

    # one of the additions to make rspec work from command line for the plugin
    config.generators do |g|
      g.test_framework :rspec, :view_specs => false
    end   
  end
end 

我已经创建了 --full 和 --mountable 引擎,并且 rspec 终于为任何阅读了一些很棒的文章的人工作(见下文),但想知道这样做对我正在尝试的解决方案的更广泛影响创建

I have already created both --full and --mountable engines and have rspec finally working for anyone reading there are some great articles (see below), but wondered of the wider impact of doing this for the solution I am trying to create

我仍在玩这个并将发布我的发现..
任何帮助/讨论将不胜感激.

I am still playing with this and will post my findings..
Any help/discussion will be massively appreciated.

请注意

  1. 为什么我要这样做 - 一次构建,多次使用...
  2. 我绝不希望非技术人员/客户添加插件/引擎"——这纯粹是为了娱乐第 1 点.

我遇到的问题...

  1. 在顶级应用上运行服务器.只有从引擎访问内容时,(我可以通过错误消息看到)我有一个路由问题(root_path 未定义或设计路由丢失) - 父应用程序布局正在呈现,我可以在提取的错误源中看到它.进步,但还没有雪茄!

有用的参考资料

推荐答案

我通过以下步骤成功解决了这个问题:

I managed to get this working with the following steps:

  1. 在我的父应用程序中,我在 routes.rb 中安装了引擎

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

mount PluginName::Engine => '/plugin_name'

我刚刚删除了它.

按照下面的 Ryan Bigg 所述,创建了一个应用程序控制器.

Created an application controller as Ryan Bigg below had stated.

class PluginName::ApplicationController < ApplicationController
  ...
end

  • 因为我希望在生成控制器、模型、测试时将名称间隔开,所以当我想要 gem 时,您必须基本上注释掉 isolate_namespace PluginName lib\plugin_name\engine.rb在父应用中运行.

  • 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.

    感谢 Ryan 帮助我找到了自己的路,非常感谢

    Kudos to Ryan for helping me find my way many thanks

    此外,同样可以使用 --mountable 开关版本完成,您需要做的就是在您的引擎配置/routes.rb 替换中进一步执行

    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
    

    Rails.application.routes.draw do
    

    这篇关于rails 3.1.1 引擎 - 使用可安装的引擎,是否可以访问父应用程序资产、默认布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆