4
votes

I'm attempting to use render html: to render raw html from a controller action:

class SomeController < ApplicationController
  def raw_html
    render html: '<html><body>Some body text</body></html>'
  end
end

However, when I run this controller action, I get a "Template is missing" error

I don't want to use a template, just render raw html.

The error I get is:

Processing by SomeController#raw_html as HTML Parameters: {} ActionView::MissingTemplate (Missing template some_controller/raw_html with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby]}. Searched in: * "/Users/doved/source/sample_app/app/views" * "/Users/doved/.rvm/gems/ruby-2.0.0-p353@syp/gems/chameleon-0.2.4/app/views" * "/Users/doved/.rvm/gems/ruby-2.0.0-p353@syp/gems/kaminari-0.15.1/app/views"): app/controllers/some_controller.rb:14:in raw_html'
lib/middleware/cors_middleware.rb:8:in
call'

I'm using Rails 4.0.2

What am I doing wrong?

2
Can you share the server log generated + error stacktrace when you get this error. - Kirti Thorat♦
Added the error / stack trace - Oved D
What is the Rails version you using? - Kirti Thorat♦
@KirtiThorat Added the rails version to my question - Oved D
I have posted an answer. Let me know if you have any doubts. - Kirti Thorat♦

2 Answers

4
votes

html option was added to render method in Rails 4.1 version.

Checkout the discussion on this topic on Github

If you upgrade the Rails version to Rails 4.1 then you would be able to render html as

def raw_html
  render html: '<html><body>Some body text</body></html>'.html_safe ## Add html_safe
end

With the current version of Rails 4.0.2, you would need to use

def raw_html
  render text: '<html><body>Some body text</body></html>' 
end

You are getting error as: ActionView::MissingTemplate

Because currently html option is not supported by render so the value passed with html option is ignored and Rails starts to look for a template some_controller/raw_html in views directory.

1
votes

Possible duplicate of

This should work for you:

render text: '<html><body>Some body text</body></html>'