Rails:渲染不起作用,仍然得到`模板丢失` [英] Rails: render doesn't work, still get `Template is missing`

查看:25
本文介绍了Rails:渲染不起作用,仍然得到`模板丢失`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习 Rails 指南.我按照步骤做了,但还是遇到了错误.

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

我的 Ruby 版本是 ruby 2.1.1p76,Rails 版本是 4.0.4.

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

我应该得到 {"title"=>"First article!", "text"=>"This is my first article."} 但结果是 >

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]}.`

这是我的相关路线:

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

<小时>

更新:render plain:Rails 4.1.0 中引入的新方法,引用 这个问题.


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

推荐答案

render方法中,Rails 4.1中增加了plain选项并且您正在使用 Rails 4.0.4.所以,rails 忽略了这个选项并开始寻找一个名为 articles/create 的模板,就像你在 ArticlesController#create 动作中一样.显然,模板不存在,因此您会收到错误Template is missing.

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.

请参阅 Gi​​thub:介绍 render :plainrender :html,让 render :body 作为 render :text 的别名

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

现在,要使用下面提到的语法,您需要升级到 Rails 4.1:

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

render plain: params[:article].inspect

使用当前版本的 Rails 4.0.4,您可以:

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

render text: params[:article].inspect

这篇关于Rails:渲染不起作用,仍然得到`模板丢失`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆