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?
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
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!"
                        render html: "hello, world!"
will do the job
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With