为什么Rails 4脚手架要创建json。jbuilder文件?

时间:2021-06-17 01:20:32

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

我使用rails新建了一个新的rails 4 (rc1)项目,并使用rails g scaffold生成了一个scaffold。

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

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

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

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

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

我认为这是为了帮助从模型生成json。控制器包含格式。json调用。

Question: why does it require json and which part of the generated application is using json? It doesn't look like (for me) the views are using json to render anything, seems they are rendered on the server side (@model variables are used in the views to get the content).

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

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

edge指南(http://edgeguides.rubyonrails.org/)没有提到jbuilder,也没有说明为什么需要它。

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

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

5 个解决方案

#1


33  

To answer the "why" part:

回答“为什么”部分:

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)

多年来,脚手架在Rails中的相关性已经有所改变。它不再是用来生成无法轻易提取的代码。今天,它主要是一个教育工具,为您提供一个动态的示例并演示最佳实践。(这也是为什么这些评论中充斥着有争议的有用性的评论)

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

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

If you are going to use Rails, here is a good way how you could do it.

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

or in your specific case:

或在你的具体情况:

If you are going to use JBuilder, here is a good way to generate JSON.

如果要使用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."

它们并不是要告诉你:“必须这样做”或“你必须保留所有生成的东西,因为这是必须的”。

#2


16  

As said before, Rails 4 seems to generate the files as a way to have a template for responding to a JSON query. So let's say you have "scaffolded" a model Car, then it will respond with a HTML page and a detail view of car (id 1) if you go /car/1.

如前所述,Rails 4似乎生成文件,作为对JSON查询进行响应的模板。假设你有一个“搭建”模型车,如果你去/ Car /1,它会响应一个HTML页面和一个详细的Car视图(id 1)。

Then if you go to /car/1.json, it will render the show.json.jbuilder file. There also seems to be some kind of automatic mechanism as the show method is empty when scaffolding:

如果你去/car/1。json将呈现show.json。jbuilder文件。似乎也有某种自动机制,因为当脚手架出现时,显示方法是空的:

def show
end

#3


14  

Rails 4 scaffold generator creates jbuilder files because the gem 'Jbuilder' is in the Gemfile. If you remove this from your Gemfile this functionality will stop.

Rails 4 scaffold生成器创建jbuilder文件是因为gem“jbuilder”位于Gemfile中。如果您从Gemfile中删除它,这个功能将停止。

#4


10  

Jbuilder gem included in RoR 4 by default. You could read about it on its page. There is a cast about it: Railscasts #320.

默认情况下,RoR 4中包含了Jbuilder gem。你可以在它的页面上看到。关于它有一个演员阵容:铁路广播#320。

#5


0  

Nowadays Angular or similar frameworks are popular or if you are writing an API for a mobile application. This JSON builder view can be used. There we will not be using Rails views. For all the rest API's JSON has became the standard format. It is useful to have both HTML view's & JSON created automatically by the rails scaffold.

现在,有棱角的或类似的框架很流行,或者如果您正在为移动应用程序编写API的话。可以使用这个JSON builder视图。在那里我们不会使用Rails视图。对于所有的rest API, JSON都成为了标准格式。由rails脚手架自动创建HTML视图和JSON是很有用的。

#1


33  

To answer the "why" part:

回答“为什么”部分:

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)

多年来,脚手架在Rails中的相关性已经有所改变。它不再是用来生成无法轻易提取的代码。今天,它主要是一个教育工具,为您提供一个动态的示例并演示最佳实践。(这也是为什么这些评论中充斥着有争议的有用性的评论)

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

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

If you are going to use Rails, here is a good way how you could do it.

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

or in your specific case:

或在你的具体情况:

If you are going to use JBuilder, here is a good way to generate JSON.

如果要使用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."

它们并不是要告诉你:“必须这样做”或“你必须保留所有生成的东西,因为这是必须的”。

#2


16  

As said before, Rails 4 seems to generate the files as a way to have a template for responding to a JSON query. So let's say you have "scaffolded" a model Car, then it will respond with a HTML page and a detail view of car (id 1) if you go /car/1.

如前所述,Rails 4似乎生成文件,作为对JSON查询进行响应的模板。假设你有一个“搭建”模型车,如果你去/ Car /1,它会响应一个HTML页面和一个详细的Car视图(id 1)。

Then if you go to /car/1.json, it will render the show.json.jbuilder file. There also seems to be some kind of automatic mechanism as the show method is empty when scaffolding:

如果你去/car/1。json将呈现show.json。jbuilder文件。似乎也有某种自动机制,因为当脚手架出现时,显示方法是空的:

def show
end

#3


14  

Rails 4 scaffold generator creates jbuilder files because the gem 'Jbuilder' is in the Gemfile. If you remove this from your Gemfile this functionality will stop.

Rails 4 scaffold生成器创建jbuilder文件是因为gem“jbuilder”位于Gemfile中。如果您从Gemfile中删除它,这个功能将停止。

#4


10  

Jbuilder gem included in RoR 4 by default. You could read about it on its page. There is a cast about it: Railscasts #320.

默认情况下,RoR 4中包含了Jbuilder gem。你可以在它的页面上看到。关于它有一个演员阵容:铁路广播#320。

#5


0  

Nowadays Angular or similar frameworks are popular or if you are writing an API for a mobile application. This JSON builder view can be used. There we will not be using Rails views. For all the rest API's JSON has became the standard format. It is useful to have both HTML view's & JSON created automatically by the rails scaffold.

现在,有棱角的或类似的框架很流行,或者如果您正在为移动应用程序编写API的话。可以使用这个JSON builder视图。在那里我们不会使用Rails视图。对于所有的rest API, JSON都成为了标准格式。由rails脚手架自动创建HTML视图和JSON是很有用的。