Isolated engine (doorkeeper) - use helper methods from the main_app

断了今生、忘了曾经 提交于 2019-12-07 09:53:37

问题


I want my doorkeeper views to use the application layout:

https://github.com/applicake/doorkeeper/wiki/Customizing-views

This contains routes and helper methods from the main application.

For the routes, I can prefix main_app to the path but for the helper method I get the following error:

undefined method `is_active?' for #<ActionDispatch::Routing::RoutesProxy:0xade808c>

<li class="<%= main_app.is_active?("high_voltage/pages", "api") %>"><%= link_to t('developers'), page_path('api') %></li>

Why is this? The helper is in app/helpers/application_helper.rb


回答1:


If you generated the views and they are placed in app/views/doorkeeper/** then the engine still uses doorkeeper controllers.

To fix this, you have to include your helper(s) into the engine's ApplicationController. Let's say you have something like this:

app/helpers/application_helper.rb

module ApplicationHelper
  def my_helper
    "hello"
  end
end

app/views/doorkeeper/applications/index.html.erb

<p>
  <%= my_helper %>
</p>

This won't work until you include your application helpers into doorkeeper controllers. So in config/application.rb:

class YourApp::Application < Rails::Application
  config.to_prepare do
    # include only the ApplicationHelper module
    Doorkeeper::ApplicationController.helper ApplicationHelper

    # include all helpers from your application
    Doorkeeper::ApplicationController.helper YourApp::Application.helpers
  end
end

this is similar configuration when you want to customize the layout.




回答2:


A helper method in application_helper.rb would not be a method for main_app.

The main_app variable is an object with a class/module of ActionDispatch::Routing::RoutesProxy.

main_app is a helper that gives you access to your application routes. main_app.page_path('api'), for example.

I'm assuming, with doorkeeper, you need to access the path you want; main_app.highvoltage_page_path('api').some_doorkeeper_active_method

This should hopefully, at least, send you in the right direction, see also:

http://edgeapi.rubyonrails.org/classes/Rails/Engine.html#label-Using+Engine%27s+routes+outside+Engine

Good luck.




回答3:


Well, this question is very old, but I ran into exactly the same problem, and I have a solution. The one requested on Felipe Elias Philipp's answer.

You need to do a little "overwrite". Copy the doorkeepers' engine application_controller.rb file to your app into app/doorkeeper/. Then just change

module Doorkeeper 
  class ApplicationController < ActionController::Base

to

module Doorkeeper 
  class ApplicationController < ::ApplicationController

Which now makes doorkeeper use your ApplicationController which probably will have all the methods you need. Together with https://github.com/doorkeeper-gem/doorkeeper/wiki/Customizing-views everything works splendidly.



来源:https://stackoverflow.com/questions/10879150/isolated-engine-doorkeeper-use-helper-methods-from-the-main-app

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