简体   繁体   English

Rails覆盖gem中的控制器布局

[英]Rails override controller layout in gem

I am using a gem that adds an engine to my Rails app with routes that all render snippets of html using the default application layout. 我使用的是gem,它使用默认应用程序布局将所有引擎渲染为html的路由添加到我的Rails应用程序中。 I want the controller in this gem to use a different layout. 我希望该gem中的控制器使用其他布局。 Is there a way I can add code to an initializer that will dynamically cause a controller in a gem to always use a layout. 有没有一种方法可以向初始化程序中添加代码,该代码将动态导致gem中的控制器始终使用布局。 Eg can I throw something like the following code (which isn't working) in an initializer assuming the full definition of SampleController is defined in the gem I am using? 例如,假设在我使用的gem中定义了SampleController的完整定义,我是否可以在初始化程序中抛出类似以下代码的代码(不起作用)?

class SampleController
  layout 'my_layout'
end

I know you can call class_eval or instance_eval for class and instance methods but how to override this type of initialization code? 我知道您可以为类和实例方法调用class_evalinstance_eval ,但是如何覆盖这种类型的初始化代码?

If you create a 'sample.html.haml' (or whichever templating language) in your layouts directory, it will be used instead of application.html for SampleController. 如果您在布局目录中创建“ sample.html.haml”(或任何模板语言),它将用于SampleController而不是application.html。

http://guides.rubyonrails.org/layouts_and_rendering.html#finding-layouts http://guides.rubyonrails.org/layouts_and_rendering.html#finding-layouts

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

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