简体   繁体   English

在Rails中使用局部

[英]Using partials in Rails

I am following this official guide to learn about Rails, where a blog is built, and I noticed that it doesn't seem to be complete. 我正在遵循此官方指南来学习有关Rails的信息,在该地方已构建了博客,并且我发现它似乎还不完整。

On Point 5.13 "Using partials to clean up duplication in views", it recommends to take the form to create a post and to edit it inside a partial, because both are so similar. 在第5.13点“使用局部视图清除视图中的重复项”上,建议使用该表格来创建帖子并在局部视图中进行编辑,因为两者是如此相似。 It does speak about a small difference about the routes that both forms use, but it says it is a problem that will be explained later 它确实说明了两种形式都使用的路线之间的细微差别,但是它说这是一个问题,稍后将进行解释。

Everything except for the form_for declaration remained the same. 除了form_for声明外,其他所有内容均保持不变。 How form_for can figure out the right action and method attributes when building the form will be explained in just a moment. 稍后将解释form_for如何在构建表单时找出正确的操作和方法属性。 [...] [...]

But I noticed the guide never actually explains how to do this. 但我注意到该指南从未真正说明如何执行此操作。 (Maybe they just forgot...?) (也许他们只是忘记了...?)

Can someone explain me how can I solve this problem? 有人可以解释我该如何解决这个问题?

If you follow the Rails conventions, you won't have to do anything. 如果遵循Rails约定,则无需执行任何操作。 This is well explained in the documentation ( http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for ). 在文档( http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for )中对此进行了很好的解释。

However, further simplification is possible if the record passed to form_for is a resource, ie it corresponds to a set of RESTful routes, eg defined using the resources method in config/routes.rb. 但是,如果传递给form_for的记录是一种资源,即它对应于一组RESTful路由,例如使用config / routes.rb中的resources方法定义,则可以进一步简化。 In this case Rails will simply infer the appropriate URL from the record itself. 在这种情况下,Rails会简单地从记录本身推断出适当的URL。

Basically, you need to: 基本上,您需要:

  1. Use form_for helper 使用form_for助手
  2. Pass model object to it (instead of a symbol) 将模型对象传递给它(而不是符号)
  3. Have defined resource in routes.rb fi resources :posts 在已定义资源routes.rb网络resources :posts

So if you have form_for(@post) , Rails will determine which route to use by checking if @post is a new record. 因此,如果您具有form_for(@post) ,Rails将通过检查@post是否为新记录来确定要使用的路由。

If you want a complete example, just create a new rails app and create any scaffold: 如果您想要一个完整的示例,只需创建一个新的Rails应用并创建任何脚手架:

rails g scaffold User name

You can check that Rails had put a form in a partial: app/views/users/_form.html.erb . 您可以检查Rails是否在部分表单中放置了表单: app/views/users/_form.html.erb

If you want to dig deep, check the source code. 如果要深入研究,请检查源代码。 The interesting part is in this file . 有趣的部分在此文件中

action, method = object.respond_to?(:persisted?) && object.persisted? ? [:edit, :patch] : [:new, :post]

In our example the @post is assigned to object . 在我们的示例中, @post被分配给object Rails is full of such short-cuts, and sometimes the best way of learning about them is looking at the source code. Rails充满了这样的捷径,有时了解它们的最佳方法是查看源代码。 If you know Ruby well, you'll often find source code to be a better explanation that the docs and guides. 如果您对Ruby熟悉,您通常会发现源代码是文档和指南的更好解释。

<%= form_for @product do |f| %>
.
.
.
<% end %>

When @product is a new record, Rails know about it and uses products_path. 当@product是新记录时,Rails会知道它并使用products_path。 However when the @product is existed one, Rails uses product_path(@product). 但是,当@product存在时,Rails使用product_path(@product)。

It doesn't do a great job of explaining it. 它并不能很好地解释它。 If you look up the page at the two versions of the form, one for new and one for edit you will see they have two different pieces of code after form_for , but they are both passing the @post variable. 如果您在表单的两个版本中查找页面,一个用于new ,一个用于edit您会看到它们在form_for之后有两个不同的代码段,但是它们都传递了@post变量。 In the first case, the variable contains a new, empty, Post object. 在第一种情况下,变量包含一个新的空Post对象。

<%= form_for :post, url: posts_path do |f| %>    

In the edit version it is looking up the record, putting it in the @post variable and passing it to the form. edit版本中,它正在查找记录,将其放入@post变量中并将其传递给表单。

<%= form_for :post, url: post_path(@post), method: :patch do |f| %>    

The final version, the partial, does not have the extra code indicating if it's a new action or edit . 最终版本(部分版本)没有额外的代码来指示它是new动作还是edit But due to the URL and the action calling the view, Rails knows which code to sub into the form when using a form partial. 但是由于URL和调用视图的操作的缘故,Rails知道使用部分表单时将哪些代码包含在表单中。 More "Rails magic". 更多“ Rails魔术”。

<%= form_for @post do |f| %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM