Rails mounted engine and overriding another engine

I am working on converting my standard Rails application into a mounted engine. The application is comparable to the standard application for blogs, and I want each model, controller and view to expand, so my choice is for the mounted engine.

One of the gems that I use is Devise, as I understand it, a bit of a mounted engine. It can be used inside the mounted engine, as indicated here .

I can use it partially in my engine. Everything works fine, including some Devise controllers I override, like this one:

# 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 of 'views / bbronline / devise_overrides / registrations / new_intermediair.html.haml' also loads correctly as expected.

However, my problem is that the views that I override without a custom controller are loaded incorrectly. For example, a view in which the login window is located in views/bbronline/devise/sessions/new.html.haml and does not load. Instead, the standard login view is loaded, i.e. devise-2.1.0/app/views/devise/sessions/new.html.erb

Of course, I could solve this problem by redefining each controller with my own controller, as I did with the registration controller above, but it seems very ugly. Does every controller turn a way to do this? Is there a more convenient way to override views from a mounted kernel from another mounted kernel?

+6
source share
3 answers

The view_paths are in the wrong order. Checking the viewing paths for Devise :: SessionsController shows:

 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 mounted engine should go to the middle of the path. The solution sets the download priority in application.rb as follows:

 #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

Thanks to Jose Valim for pointing in the right direction.

+4
source

If you do not want to configure config.railties_order in every application that uses your engine, simply require 'devise' on top of your lib \ my_engine \ engine.rb file .

+7
source

I need more information. Which controller do you define and from which controller does it inherit? Which view is visible and which one did you expect to give away? Also .view_paths is your friend, so try the following in the rails console:

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

This will give you a better idea of ​​where each controller is looking for patterns.

+1
source

Source: https://habr.com/ru/post/919313/


All Articles