“产量”有什么作用?

What does “yield” do?

yield 在 rails 上 ruby 做什么?

<body data-spy="scroll" data-target=".sidebar">
  <!-- Your timezone is <%= Time.zone %> -->
  <!-- <%= "Ruby Version is #{RUBY_VERSION}" if Rails.env =~ /test|development/ %> -->
  <%= render partial:'shared/account_status' %>
  <%= render partial:"shared/session_timeout" %>
  <div class="container">
    <%= render partial:"shared/branding" %>
    <%= render partial:"shared/nav", locals:{icons:icons, actionable_urls:actionable_urls, top_level_items:MenuItem.top_level_items_with_access_rights_for_user(current_user).sort{|a, b| a.sequence <=> b.sequence}, current_item:current_navigation_item} %>
    <div style="clear:both"></div>
    <div id="content">
      <%= render partial:"shared/flash", object:flash %>
      <%= yield %>
    </div>

  </div>
  <%= render partial:"shared/ldap_user_menu" if signed_in_as_ldap_user?  %>
</body>

它告诉 Rails 将您的视图内容放入布局文件中该位置的此块(由 yield 调用)。

查看 rails 指南以了解有关 ActionView 的更多信息。
https://guides.rubyonrails.org/action_view_overview.html

正如@Aleksei Matiushkin 所指出的,yield 是纯粹的 ruby,因此您也应该在自己的时间了解更多。

这里有一个 (my) 视觉演示来解释那条线上发生的事情:

view.html.erb:

<p>Hello there!</p>
<p>I'm a content from view</p>

layout.html.erb:

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <%= yield %>
  </body>
</html>

现在的结果是这样的:

<!DOCTYPE html>
<html>
  <head>
  </head>

  <body>
    <p>Hello there!</p>
    <p>I'm a content from view</p>
  </body>
</html>

你的问题不够具体。在 Rails 中,与普通的 Ruby 一样,在方法定义中使用的 yield 表示传递给方法的块。

但是,从您提供的代码块来看,您似乎特别想问一下Rails中视图布局中使用的yield。在这种情况下,它表示要在上下文中呈现的视图文件中描述的主要内容。例如,当控制器是 Foo,而动作是 bar,那么,在布局中使用的 yield 将被替换为 [=15] 的内容=](或任何其他格式的相应视图文件)。

您的概念一定很简单:yield 是将内容块 (html) 从操作视图放置到布局模板的位置 示例:动作索引渲染 index.html,结果将是 put/filled 在 yield

我没有在其他答案中看到 render,所以我想补充一下@vipibano 的答案,如果您想了解这个 yield 业务在 Rails(更具体地说,在 application.html.erb 中),您将受益于:

  1. 了解块在 Ruby 中的工作原理(如其他人所述)
  2. 了解 Rails' render 方法的作用

render 方法在控制器操作结束时调用,并协调将哪个块传递给实际呈现 application.html.erb 的方法。

我已经尝试将其形象化一些,如果您想深入了解 post:

,可以举几个例子

https://richstone.io/debunk/