felixa
felixa

Reputation: 21

Rendering a file/template with layout outside of controller with Rails 4

Currently I am rendering a view in Rails 4 outside of a Controller with this code:

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: {}
)

How can I render the view inside of a layout? The following does not work:

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

Thanks!

Upvotes: 1

Views: 798

Answers (1)

felixa
felixa

Reputation: 21

I solved it. It works fine as expected:

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

What does not work is:

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

looks like it searches for a layout layouts/application.pdf.erb or similar. It does fail silently though.

Upvotes: 1

Related Questions