隔离引擎(门卫) - 使用main_app中的帮助器方法 [英] Isolated engine (doorkeeper) - use helper methods from the main_app
问题描述
我希望我的看门人视图能够使用应用程序布局:
https://github.com/applicake/doorkeeper/wiki/Customizing-views
这包含路线和辅助方法来自主应用程序。
对于路由,我可以将main_app前缀添加到路径,但对于辅助方法,我得到以下错误:
未定义方法`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>
这是为什么?帮助程序位于 app / helpers / application_helper.rb中
您生成了视图,并且它们被放置在 app / views / doorkeeper / **
中,然后引擎仍然使用守门员控制器。
为了解决这个问题,你必须将你的助手包含到引擎的 ApplicationController
中。假设你有这样的事情:
app / helpers / application_helper.rb
module ApplicationHelper
def my_helper
hello
end
end
app / views / doorkeeper / applications / index.html.erb
< p>
<%= my_helper%>
< / p>
直到您将应用程序帮助程序添加到门卫控制器中后,才会生效。所以在 config / application.rb
:
class YourApp :: Application < Rails :: Application
config.to_prepare do
#仅包含ApplicationHelper模块
Doorkeeper :: ApplicationController.helper ApplicationHelper
#包含应用程序中的所有帮助程序
Doorkeeper :: ApplicationController.helper YourApp :: Application.helpers
end
end
这是类似的配置,当你想自定义布局。
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
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.
这篇关于隔离引擎(门卫) - 使用main_app中的帮助器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!