Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 329
Hao Tan Avatar asked Apr 10 '14 06:04

Hao Tan


People also ask

How can you tell Rails to render without a layout *?

By default, if you use the :plain option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the layout: true option and use the . text. erb extension for the layout file.

What is the partial render in the rails?

Rails Guides describes partials this way: Partial templates - usually just called "partials" - are another device for breaking the rendering process into more manageable chunks. With a partial, you can move the code for rendering a particular piece of a response to its own file.

What is render in Ruby on Rails?

Rendering is the ultimate goal of your Ruby on Rails application. You render a view, usually . html. erb files, which contain a mix of HMTL & Ruby code. A view is what the user sees.


1 Answers

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 
like image 93
Kirti Thorat Avatar answered Sep 21 '22 12:09

Kirti Thorat