Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 824
Gianmarco Digiacomo Avatar asked Jul 02 '17 17:07

Gianmarco Digiacomo


3 Answers

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

like image 35
Sujan Adiga Avatar answered Nov 11 '22 17:11

Sujan Adiga


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!"
like image 106
Pavan Avatar answered Nov 11 '22 17:11

Pavan


render html: "hello, world!"

will do the job

like image 20
mrateb Avatar answered Nov 11 '22 17:11

mrateb