Respond to Controller action request without html/head tags

This is very basic and very difficult to search for exactly what I’m trying to do but…

If I want a specific controller action to render and respond with just a rendered partial, and not also include the obligatory <html><head><body> etc tags, how can I accomplish this?

When setting layout:false it will not add my application.html.erb layout, but it still responds with an html object even when responding to JS format.

I’m guessing I need to use a render text, but how can I return a render partial as the reponse of a render text?

If you want to render a template or a partial and return it as text, you can use render_to_string render_to_string (AbstractController::Rendering) - APIdock and then respond with that text

Something like:

rendered = render_to_string partial: 'my_partial', locals: {some_var: my_var}, layout: false
render plain: rendered # or html: rendered
1 Like

Thank you that does the trick and was what I was looking for!