简体   繁体   English

Rails可安装发动机并覆盖另一个发动机

[英]Rails mountable engine and overriding another engine

I'm in the proces of converting my standard Rails app to an mountable engine. 我正处于将标准Rails应用程序转换为可安装引擎的过程中。 The app is comparable to a standard blogging app and i want every model, controller and view to be extendable hence my choice for a mountable engine. 该应用程序可与标准博客应用程序相媲美,我希望每个模型,控制器和视图都可以扩展,因此我选择了可​​安装的引擎。

One of the gems i use is Devise which is as far as i understand a sort of a mountable engine itself. 我使用的宝石之一是Devise,据我所知,这是一种可安装的引擎本身。 It can be used inside a mountable engine as stated here . 它可以在这里所述的可安装发动机内使用。

I'm able to use it partially within my engine. 我可以在我的引擎中部分使用它。 Everything is working fine including some Devise controller i override like this one: 一切都很好,包括一些Devise控制器我覆盖像这样:

# config/routes.rb

Bbronline::Engine.routes.draw do
  devise_for :users, class_name: "Bbronline::User", module: :devise,
    controllers: { registrations: "bbronline/devise_overrides/registrations"}
    ...

# controllers/bbronline/devise_overrides/registrations_controller.rb
require_dependency "bbronline/application_controller"

module Bbronline

class DeviseOverrides::RegistrationsController < Devise::RegistrationsController

  def new_intermediair
    @user = User.new
  end
  ...

The correct view 'views/bbronline/devise_overrides/registrations/new_intermediair.html.haml' is also correctly loading as expected. 正确的视图'views / bbronline / devise_overrides / registrations / new_intermediair.html.haml'也按预期正确加载。

However my issue is that the views that i override without a custom controller are not properly loaded. 但是我的问题是我没有自定义控制器覆盖的视图没有正确加载。 For example the view that should the login view is located in views/bbronline/devise/sessions/new.html.haml and is not loaded. 例如,登录视图应位于views/bbronline/devise/sessions/new.html.haml中且未加载的views/bbronline/devise/sessions/new.html.haml Instead the standard Devise login view gets loaded ie devise-2.1.0/app/views/devise/sessions/new.html.erb 而是加载标准的Devise登录视图,即devise-2.1.0/app/views/devise/sessions/new.html.erb

Of course i could solve this problem by overriding every controller with my own controller like i did with the registrations_controller above but this seems very ugly. 当然我可以通过用我自己的控制器覆盖每个控制器来解决这个问题,就像我上面的registrations_controller一样,但这看起来非常难看。 Is overriding every controller the way to do this? 是否覆盖每个控制器的方式? Is there a more convenient way to override views from an mountable engine from within another mountable engine? 是否有更方便的方法从另一个可安装引擎中覆盖可安装引擎的视图?

如果您不想在使用引擎的每个应用程序中调整config.railties_order ,只需在lib \\ my_engine \\ engine.rb文件的顶部require 'devise'

The view_paths are in incorrect order. view_paths的顺序不正确。 Checking the view paths of Devise::SessionsController shows: 检查Devise :: SessionsController的视图路径显示:

Devise::SessionsController.view_paths 
=> #<ActionView::PathSet:0x007fa1bf0e36f8 @paths= [/Users/harmdewit/Dropbox/Code/projects/brightin/bbr-online/bbr-online-gem/test/‌​dummy/app/views, 
/Users/harmdewit/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/devise-2.1.‌​0/app/views, 
/Users/harmdewit/Dropbox/Code/projects/brightin/bbr-online/bbr-online-gem/app/vi‌​ews]> 

The last path of the mountable engine should come before the middle devise path. 可安装引擎的最后一条路径应该位于中间设计路径之前。 The solution is setting the loading priority in application.rb like this: 解决方案是在application.rb中设置加载优先级,如下所示:

#test/dummy/config/application.rb (the app that uses my mountable engine)
...
config.railties_order = [Blog::Engine, :main_app, :all]
...

This is also documented in the rails api: http://api.rubyonrails.org/classes/Rails/Engine.html#label-Loading+priority 这也记录在rails api中: http//api.rubyonrails.org/classes/Rails/Engine.html#label-Loading+priority

Thanks to José Valim for pointing in the right direction. 感谢JoséValim指向正确的方向。

I need more information. 我需要更多信息。 Which controller are you defining and from which controller is it inheriting from? 您定义了哪个控制器以及它从哪个控制器继承? Which view is being rendered and which one did you expect to render? 正在渲染哪个视图以及您希望渲染哪个视图? Also, .view_paths is your friend so try in your rails console the following: 另外, .view_paths是你的朋友,所以请在rails控制台中尝试以下操作:

Devise::SessionsController.view_paths
YourApp::SomeDeviseController.view_paths

This will give you a better idea of where each controller is searching for templates. 这将使您更好地了解每个控制器搜索模板的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM