本文介绍了为什么 Rails 4 脚手架会创建 json.jbuilder 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 rails new 生成了一个新的 rails 4 (rc1) 项目,并使用 rails g scaffold 生成了一个脚手架.

I've generated a new rails 4 (rc1) project using rails new and generated a scaffold using rails g scaffold.

正如预期的那样,它已经创建了迁移、控制器和所有必需的视图文件.

As expected it has created the migration, controller and all required view files.

它还创建了 show.json.jbuilder 和 index.json.jbuilder.

It has also created show.json.jbuilder and index.json.jbuilder.

我认为这是为了帮助从模型生成 json.控制器也包含 format.json 调用.

I assume this is to aid json generation from models. The controller contains format.json invocations as well.

问题:为什么需要json,生成的应用程序的哪一部分使用的是json?它看起来不像(对我来说)视图使用 json 来呈现任何东西,似乎它们是在服务器端呈现的(@model 变量用于视图中以获取内容).

边缘指南 (http://edgeguides.rubyonrails.org/) 没有看到提及 jbuilder 以及为什么需要它

The edge guides (http://edgeguides.rubyonrails.org/) don't see to mention jbuilder and why it's needed there.

提前致谢!如果我能澄清这个问题,请告诉我.

Thanks in advance! Please let me know if I can clarify the question.

推荐答案

回答为什么"部分:

多年来,Rails 中脚手架的相关性发生了一些变化.它不再意味着生成无法轻松抽象的必要代码.今天,它主要是一个教育工具,为您提供一个有点动态的例子并展示最佳实践.(这也是为什么他们在评论中散布着有争议的有用性的原因)

The relevance of scaffolding in Rails has somewhat shifted over the years. It is no longer meant to generate necessary code which couldn't be abstracted away easily. Today it is mostly an educational tool to provide you with a somewhat dynamic example and demonstrate best practices. (That's also the reason why they are sprinkled with comments with disputable usefulness)

换句话说,生成的文件是为了告诉你:

In other words the generated files are meant to tell you:

如果您打算使用 Rails,这里有一个很好的方法.

或在您的特定情况下:

如果您打算使用 JBuilder,这里有一个生成 JSON 的好方法.

他们不是想告诉:这是必须要做的."或者你必须保留所有生成的东西,因为它是必要的."

They are not meant to tell: "This is how it must be done." or "You have to keep all the generated stuff because it's necessary."

这篇关于为什么 Rails 4 脚手架会创建 json.jbuilder 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 19:32