Hao Tan
Hao Tan

Reputation: 1638

Rails: render doesn't work, still get `Template is missing`

I am currently learning Rails Guides. I went through the steps but still encountered a mistake.

My Ruby version is ruby 2.1.1p76 and the Rails version is 4.0.4.

As the guide directed, I created an Article Controller.

class ArticlesController < ApplicationController
  def new
  end

  def create
    render plain: params[:article].inspect
  end

end

I should get {"title"=>"First article!", "text"=>"This is my first article."} but the output turned out to be

Template is missing
Missing template articles/create, application/create with {:locale=>[:en], :formats=>[:html],    :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}.`

Here is my related routes:

articles GET    /articles(.:format)          articles#index
         POST   /articles(.:format)          articles#create

Update: render plain: is a new method introduced in Rails 4.1.0 referred to this issue.

Upvotes: 29

Views: 14018

Answers (5)

Victor
Victor

Reputation: 947

Change Rails version in your Gemfile:

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.6'

Then run:

bundle install

Make sure that your Rails version now is > 4.1

Upvotes: 0

Kirti Thorat
Kirti Thorat

Reputation: 53048

In the render method, plain option was added in Rails 4.1 and you are using Rails 4.0.4. So, rails ignored this option and started looking for a template named articles/create as you are in ArticlesController#create action. Obviously, the template doesn't exist so you get the error Template is missing.

Refer to the discussion on this topic on Github: Introduce render :plain and render :html, make render :body as an alias to render :text

Now, for using the below mentioned syntax you would need to upgrade to Rails 4.1:

render plain: params[:article].inspect

With your current version of Rails 4.0.4, you can go for:

render text: params[:article].inspect

Upvotes: 53

Jenorish
Jenorish

Reputation: 1714

You no need template means you could use render nothing: true

Try like this:

class ArticlesController < ApplicationController
 def new
 end

 def create
  params[:article].inspect
  render nothing: true
 end
end

Please refer this link click here

Upvotes: -1

Kalpesh Fulpagare
Kalpesh Fulpagare

Reputation: 1299

If you want to see textual information of params[:article] on your page then you can use render text

try this

class ArticlesController < ApplicationController
  def new
  end

  def create
    render text: params[:article].inspect
  end    
end

You will get

{"title"=>"First article!", "text"=>"This is my first article."}
# i.e. your params(whatever params hash contains)

Upvotes: 3

Pierre-Louis Gottfrois
Pierre-Louis Gottfrois

Reputation: 17631

You may want to read through the following documentation.

Rendering pure text is most useful when you're responding to Ajax or web service requests that are expecting something other than proper HTML.

Upvotes: -2

Related Questions