How to change a template location for render_to_string method?

264 Views Asked by At

I know render_to_string method searches a template in views folder by default, but can this be changed? I mean, does the template have to be located in the views folder or not? I don't like to have some XML templates in the views.

Now my template is in views/domains/fulfillment/template.xml.erb, but I would prefer not to have it in views like app/domains/fulfillment/template.xml.erb.

My current code:

def query(params:, file_path:)
    ActionController::Base.new.render_to_string(
      layout: false,
      template: "app/domains/fulfillment/#{file_path}",
      locals: params,
    ).strip
  end

I am getting an ActionView::MissingTemplate error now, when the its not in the views folder.

I have tried only to changed the template parameter in render_to_string method and can't find how to change it. Thanks.

1

There are 1 best solutions below

1
max On

You can use the file: option to specify an absolute path to a file instead of using the view paths:

ActionController::Base.new.render_to_string(
  layout: false,
  file: Rails.root.join("app", "domains", "fulfillment", file_path)
  locals: params,
).strip