I have Rails API app. And 99% of the routes are JSON routes. however, I want to add one single route that will response with HTML. How can I do that?
This is my current setup and when I browse the route I see a string of HTML tags on the screen.
class ApplicationController < ActionController::API
include ActionController::MimeResponds
end
class DocumentPublicController < ApplicationController
respond_to :html
def show
html = "<html><head></head><body><h1>Holololo</h1></body></html>"#, :content_type => 'text/html'
respond_to do |format|
format.html {render html: html, :content_type => 'text/html'}
end
end
end
Any ideas?
According to the Layouts and Rendering Guide:
When using html: option, HTML entities will be escaped if the string is not marked as HTML safe by using html_safe method.
So you just need to tell it the string is safe to render as html:
# modified this line, though could be done in the actual render call as well
html = "<html><head></head><body><h1>Holololo</h1></body></html>".html_safe
respond_to do |format|
format.html {render html: html, :content_type => 'text/html'}
end