5

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?

11
  • 1
    Can you share the server log generated + error stacktrace when you get this error. Commented Apr 9, 2014 at 21:47
  • Added the error / stack trace Commented Apr 9, 2014 at 21:55
  • What is the Rails version you using? Commented Apr 9, 2014 at 21:57
  • @KirtiThorat Added the rails version to my question Commented Apr 9, 2014 at 21:58
  • 1
    I have posted an answer. Let me know if you have any doubts. Commented Apr 9, 2014 at 22:27

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Possible duplicate of

This should work for you:

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

2 Comments

this does work, and I could output a content type to indicate it's html, but not exactly what I want to do
actually - putting content type wasn't necessary, I was able to just render the html, and text/html came as the content type automatically

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.