MyEngine::FrontendController#index 缺少请求格式的模板:text/html 呈现与控制器不同的布局

问题描述 投票:0回答:1

布局存在于 app/apps/themes/default_theme/views/layouts 中 我有一个前端控制器,它根据参数呈现不同主题的布局。现在,我们假设它呈现default_theme。但是如何设置默认主题呢?

module MyEngine
  class FrontendController < ApplicationController
    before_action :set_theme_view_path
    layout :determine_layout

    def index
    end

    private
    
    def set_theme_view_path
      theme =  'default_theme'
      # Add both the layouts and views paths for the theme
      theme_layout_path = MyEngine::Engine.root.join('app', 'apps', 'themes', theme, 'views', 'layouts')
      theme_view_path = MyEngine::Engine.root.join('app', 'apps', 'themes', theme, 'views')
      prepend_view_path(theme_layout_path)
      prepend_view_path(theme_view_path)
    end

    def determine_layout
      theme =  'default_theme'
      "layouts/application"
    end
  end
end

如何在运行时动态渲染布局?就我而言,只是另一个文件夹中的特定布局,而不是视图文件夹内的常规布局?

ruby-on-rails layout ruby-on-rails-7 rails-engines actionviewhelper
1个回答
0
投票

您无需添加

theme_layout_path
。布局名称应相对于
views/layouts
:

before_action :set_theme_view_path
layout :determine_layout

private

def set_theme_view_path
  theme = "default_theme"
  prepend_view_path MyEngine::Engine.root.join("app/apps/themes/#{theme}/views")
end

def determine_layout
  # "app/apps/themes/default_theme/views/layouts/default_theme_layout.html.erb"
  "default_theme_layout"
end
© www.soinside.com 2019 - 2024. All rights reserved.