Gianmarco Digiacomo
Gianmarco Digiacomo

Reputation: 312

railstutorial ruby on rails Missing template in tutorial

I'm in the very beginning of my career as a Ruby on Rails developer I'm reading online a book named "Ruby on Rails Tutorial (Rails 5) Learn Web Development with Rails" I've made an 'hello world' app following the book instructions.

app/controllers/application_controller.rb

class ApplicationController < ActionController::Base

  protect_from_forgery with: :exception

  def hello
    render text: "hello world!"
  end

end

config/routes.rb

Rails.application.routes.draw do

  root 'application#hello'

end

Now I receive that error

Missing template application/hello with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html,

:builder, :ruby, :coffee, :jbuilder]}. Searched in: app_path/app/views

I have

/app/view/layouts/application.html.erb

in my project so that should theoretically be the view, shouldn't it?

So am I missing something? How could I fix it?

Upvotes: 8

Views: 5441

Answers (3)

mrateb
mrateb

Reputation: 2509

render html: "hello, world!"

will do the job

Upvotes: 1

Pavan
Pavan

Reputation: 33542

Missing template application/hello with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html,:builder, :ruby, :coffee, :jbuilder]}. Searched in: app_path/app/views

In addition to @Sujan Adiga's answer, render :text misdirect people to think that it would render content with text/plain MIME type. However, render :text actually sets the response body directly, and inherits the default response MIME type, which is text/html. So Rails tries to find the HTML template and spits out with that error if unable to find.

To avoid this, you can either use content_type option to set the MIME type to text/plain or just use render :plain

render text: "hello world!", content_type: 'text/plain'

or

render plain: "hello world!"

Upvotes: 8

Sujan Adiga
Sujan Adiga

Reputation: 1371

Try

render plain: "hello world!"

when you do render text: ..., it tries to render template with name hello.erb|haml|jbuilder|... and passes text= "hello world!" as data.

Ref

Upvotes: 5

Related Questions