I'm trying to create an HTML string using a view. I would like to render this from a class that is not a controller. How can I use the rails rendering engine outside a controller? Similar to how ActionMailer does?
Thanks!
Rails 5 and 6 support this in a much more convenient manner that handles creating a request and whatnot behind the scenes:
rendered_string = ApplicationController.render(
template: 'users/show',
assigns: { user: @user }
)
This renders app/views/users/show.html.erb
and sets the @user
instance variable so you don't need to make any changes to your template. It automatically uses the layout specified in ApplicationController
(application.html.erb
by default). Full documentation can be found here.
The test shows a handful of additional options and approaches.
views
directory.
Dec 3, 2017 at 14:21
ActionView::Base.new('app/views').render(file: 'invoices/show', locals: {invoiceObjIn: inv}, layout: 'layouts/invoice')
Aug 8, 2018 at 16:23
Devise could not find the 'Warden::Proxy' instance on your request environment.
. How do you solve this?
Sep 4, 2018 at 11:42
You can use ActionView::Base to achieve this.
view = ActionView::Base.new(ActionController::Base.view_paths, {})
view.render(file: 'template.html.erb')
The ActionView::Base initialize takes:
assigns
hash, providing the variables for the templateIf you would like to include helpers, you can use class_eval to include them:
view.class_eval do
include ApplicationHelper
# any other custom helpers can be included here
end
view.render(file: 'template.html.erb', layout: 'layouts/layout.html.erb')
Jun 19, 2018 at 12:25
In Rails 5:
view = ActionView::Base.new(ActionController::Base.view_paths)
view.render(file: 'template.html.erb')
In Rails 6.1:
lookup_context = ActionView::LookupContext.new(ActionController::Base.view_paths)
context = ActionView::Base.with_empty_template_cache.new(lookup_context, {}, nil)
renderer = ActionView::Renderer.new(lookup_context)
renderer.render(context, { file: 'app/views/template.html.erb' })
ActionView::Renderer.new()
takes a lookup_context
arg, and render()
method takes a context
, so we set those up firstActionView::Base
is the default ActiveView context, and must be initialized with with_empty_template_cache
method, else render()
will error{}, nil
are required assigns
and controller
args, which used to default to {}, nil
in Rails 5file: 'app/views/template.html'
, whereas Rails 5 only required the filenameThere is no need to over bloat your app with too many gems. As we know ERB is already included in your Rails app.
@jdf = JDF.new
@job = ERB.new(File.read(Rails.root + "app/views/entries/job.xml.erb"))
result = @job.result(binding)
Above there is snippet of code of an app I'm working on.
@jdf
is a object to be evaluated in the erb
view. xml
.result
is a string to be saved or sent anywhere you like."I'm trying to create an HTML string using a view." -- If you mean you're in the context of a view template, then just use a helper method or render a partial.
If you're in some other "Plain Old Ruby Object", then keep in mind you're free to use the ERB module directly:
erb = ERB.new("path/to/template")
result = erb.result(binding)
The trick is getting that 'binding' object that gives the context for the code in the template. ActionController and other Rails classes expose it for free, but I couldn't find a reference that explains where it comes from.
http://www.ruby-doc.org/stdlib-2.2.0/libdoc/erb/rdoc/ERB.html#method-i-result
Calling directly render method on ApplicationController may raise error
Helper Devise: could not find the `Warden::Proxy` instance on request environment
Instead we can use ApplicationController.renderer.render like
rendered_string = ApplicationController.renderer.render(
partial: 'users/show',
locals: { user: user }
)
Technically, ActionMailer
is a subclass implementation of AbstractController::Base
. If you want to implement this functionality on your own, you'll likely want to inherit from AbstractController::Base
as well.
There is a good blog post here: https://www.amberbit.com/blog/2011/12/27/render-views-and-partials-outside-controllers-in-rails-3/ that explains the steps required.
For future reference, I ended up finding this handy gem that makes this a breeze:
Best to render the view using a controller, as that's what they're for, and then convert it to a string, as that is your ultimate goal.
require 'open-uri'
html = open(url, &:read)