使用Rails 4渲染控制器外部布局的文件/模板

时间:2016-04-07 11:59:12

标签: ruby-on-rails ruby-on-rails-4

目前,我正在使用以下代码在Controller外部的Rails 4中呈现视图:

av = ActionView::Base.new(ActionController::Base.view_paths, {}, ActionController::Base.new)
av.config = Rails.application.config.action_controller
av.class_eval do
  include ApplicationHelper
end

html = av.render(
  file: '/users/index',
  locals: {}
)

如何在布局中渲染视图? 以下不起作用:

html = av.render(
  file: '/users/index',
  locals: {}
  layout: 'layouts/application' 
)

谢谢!

1 个答案:

答案 0 :(得分:1)

我解决了。它按预期工作正常:

html = av.render(
  file: '/users/index',
  locals: {}
  layout: 'layouts/application' 
)

什么行不通:

html = av.render(
  file: '/users/index.pdf',
  locals: {}
  layout: 'layouts/application' 
)

看起来像是在搜索布局layouts/application.pdf.erb或类似内容。它确实无声地失败。