I am trying to render from outside a controller exactly what my Rails app renders from within a controller when a user sends an HTTP request with specific headers and parameters.
ApplicationController.renderer
is pretty good for this, however it requires me to pass the specific instance variables the template will need to render. This does not work for me as it skips the logic my app uses to determine the assigns
the template will need.
For example, my app has a before_filter
to determine which language to use from the HTTP_ACCEPT_LANGUAGE
header and send that language to the template. I don't want to have to duplicate this logic outside the controller where I want to render to a string.
One "solution" that works here is to programmatically send an actual HTTP request to my app with the appropriate headers and so forth but this is obviously inefficient.
What I'd like instead is to be able to do something like this:
ApplicationController.renderer.new({
params: {
q: 'search term'
},
request: {
env: { 'HTTP_ACCEPT_LANGUAGE': "en" }
}
}).render(inline: "<%= params[:q] %> in lang <%= request.env['HTTP_ACCEPT_LANGUAGE'] %>")
Is this possible?