Rails 中的 render 和 yield 有什么区别 [英] What is the difference between render and yield in Rails

查看:52
本文介绍了Rails 中的 render 和 yield 有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以用 <% 解释<%= render %>"和<%= yield %>"之间的区别content_for :partial do %>/<% end %>"?特别是从一个切换到另一个时路由如何变化,使用一个的好处在另一个上,什么时候使用一个而不是另一个.这个是我找到的最接近的解释,但对我来说还不够清楚.

我已经尝试了几天来解决这个问题,但似乎我尝试的每个配置要么接近,要么出错.

如果有 aaabbbccc 三个视图,并且每个视图都有一个 index.html.erb,但是 bbbccc 有一个 _content.html.erb 部分(由下划线表示)你怎么能完成获得 bbbccc 部分在 aaa 中使用 renderyield?

以下工作:

aaa 的 index.html.erb :

<%= 渲染 'bbb/content' %>

bbbs _content.html/erb :

内容来自 bbb.</p>

但这不会:

aaa 的 index.html.erb :

<%=产率:容器%>

bbbs _content.html/erb :

<% content_for :container do %><p>来自bbb的内容.</p>###在aaa中查看<%结束>

ccc 的 _content.html.erb 什么都没有,或者 content_for,但我仍然没有得到 aaa 的索引.要填充内容的 html.

如果我使用渲染,我可以明确地将内容放入.但我认为使用 yield :whatever 的好处将允许我选择填充它的内容,我可以一旦我将它从渲染更改为产量,就不会让它填充任何内容.我还必须更新路由文件吗?如果是这样,我该如何选择填充它?这是否意味着它在控制器中?需要采取行动?

我也认为这取决于最初路由到哪个文件,但就像我说的那样,我想我需要先了解两者之间的区别,然后才能开始使用部分来发挥我的优势.

解决方案

首先,yield 是 ruby​​,render 是 rails.通常对应用程序使用通用布局,其内部内容根据操作/上下文而变化.问题通常在于定义我们的布局结束和上下文特定模板的开始位置.例如,HTML 标题标签.假设您有一个名为 Cities 的应用程序.在大多数情况下,您希望页面标题始终为城市".但是,例如,如果您在阿姆斯特丹页面内,那么您希望将阿姆斯特丹"作为页面标题.

# application.html.erb<头><%= content_for?(:page_title) ?产量(:page_title):城市"%>......# city/index.html.erb<% content_for :page_title do %><%= @city.name %><%结束%><div class="bla"...

在 Rails 中,您通常在应用程序布局中定义应用程序标题.更改页面标题的一种策略是在特定城市模板中使用 content_for 并进行相应更改.

另一方面,Render 完成不同的渲染策略.直的.当您调用 render 时,它会进行渲染.content_for/yield 不会自动渲染,它存储在某处,然后在适当的位置填充缺失的位置.因此,与渲染相比,您可以将其更多地视为存储/搜索/替换",而渲染只是简单的渲染.

一个优于另一个的良好经验法则是:如果您正在编写的模板需要针对每个上下文呈现不同的信息,请强烈考虑使用 content_for.

Can someone explain the difference between "<%= render %>" and "<%= yield %> with <% content_for :partial do %>/<% end %>"? specifically how the routing changes when switching from one to another, the benefits of using one over the other, when is it practical to use one over the other. THIS is the closest explanation I have found, but isn't quite clear enough for me.

I have been trying for several days to wrap my head around this, but it seems that each configuration I try either comes close, or errors out.

If theres are three views, aaa and bbb and ccc, and each has an index.html.erb, but bbb and ccc have a _content.html.erb partial (signified by the underscore) how can you accomplish getting the bbb or ccc partial in aaa using either render or yield?

The following works:

aaa's index.html.erb :

<div">
  <%= render 'bbb/content' %>
</div>

and bbbs _content.html/erb :

<p>Content from bbb.</p>  

BUT this does NOT:

aaa's index.html.erb :

<div">
  <%= yield :container %>
</div>

and bbbs _content.html/erb :

<% content_for :container do %>
  <p>Content from bbb.</p>   ### viewed in aaa
<% end>

and cccs _content.html.erb would have nothing, or the content_for, but I still dont get aaa's index.html to be populated with content.

If I use the render, I can explicitly place the content in. But I thought that the benefit of using the yield :whatever would allow me to choose what to populate it with, and I can't get it to populate anything as soon as I change it from render to yield. Do I also have to update the routes file? If so, how do I choose which one to populate it with? Does that mean its in the controller? and needs an action?

I also have though that it depends on which file is initially routed to, but like I said, I think I need to understand the difference between the two before I can begin to use the partials to my advantage.

解决方案

First of all, yield is ruby, render is rails. Usually one uses a common layout for the application whose inner content changes according to action/context. The problem usually lies in defining where our layout ends and context-specific template begins. Take, for instance, the HTML title tag. Let's say you have an application called Cities. In most cases, you want your page title to be "Cities" all the time. But, if you're for instance, inside Amsterdam page, then you would like the have "Amsterdam" as your page title.

# application.html.erb
<html>
  <head>
    <%= content_for?(:page_title) ? yield(:page_title) : "Cities" %>
    ......

# city/index.html.erb
<% content_for :page_title do %>
  <%= @city.name %> 
<% end %>

<div class="bla"...

Within Rails you usually define your application title in your application layout. One strategy for changing the page title would be to use content_for in the specific cities template and change accordingly.

Render, on the other hand, accomplishes different rendering strategies. Straight. When you call render, it renders. content_for/yield doesn't render automatically, it is stored somewhere and then fills up the missing spots in the due places. So, you can think of it as more as a "store/search/replace" in comparison to render, which just plain renders.

Good rule of thumb to use one over the other is: if the template you are writing needs to present different information per context, strongly consider using content_for.

这篇关于Rails 中的 render 和 yield 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆