Ruby-on-rails Render 与 Render Partial 和 Yield 之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16822775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:04:36  来源:igfitidea点击:

Difference between Render and Render Partial and Yield

ruby-on-railsruby-on-rails-3.2

提问by MrYoshiji

I have read it from the Rails guides, Have looked at Micheal Hartel book and now reading it from Rails View book but still I get confused :(

我已经从 Rails 指南中阅读过它,看过 Micheal Hartel 的书,现在从 Rails View 书中阅读,但我仍然感到困惑:(

There is a _footer.html.erbfile so it is a "partial" and in the code it has written:

有一个_footer.html.erb文件,因此它是“部分”文件,并且在它编写的代码中:

<%=render 'layouts/footer' %>

so my understanding is that when it sees this, goes and insert the HTML for footer file in here. Ok... Now a few pages later it is saying:

所以我的理解是,当它看到这一点时,会在此处插入页脚文件的 HTML。好的......现在几页后它说:

<%= render partial: 'activitiy_items/recent' %>

so WHY this time we have the word "partial" in here but we didn't have it in the previous one?

那么为什么这次我们在这里有“部分”这个词,而在上一次没有呢?

And there somewhere else I see <%= yield :sidebar %>

在其他地方我看到 <%= yield :sidebar %>

So this yieldalso insert HTML in its place? Well wasn't it what renderwas doing?

所以这yield也会在它的位置插入 HTML 吗?这不是render在做什么吗?

I was hoping if another programmer instead of books explains this to me maybe I get it this time:)

我希望如果另一个程序员而不是书籍向我解释这一点,也许这次我明白了:)

回答by MrYoshiji

render& render partial:

render& render partial:

  • render 'some_view'is a shorthand for render partial: 'some_view'.
  • render file: 'view'will look for a file view.html.erband NOT _view.html.erb(.erbor any other renderer you use)
  • renderwill not accept additional local variables for the partial, you need to use render partial:as following for that:

    render partial: 'some/path/to/my/partial', locals: { custom_var: 'Hello' }
    
  • render 'some_view'是 的简写render partial: 'some_view'
  • render file: 'view'将查找文件view.html.erb而不是_view.html.erb.erb或您使用的任何其他渲染器)
  • render不会为部分接受额外的局部变量,你需要使用render partial:如下:

    render partial: 'some/path/to/my/partial', locals: { custom_var: 'Hello' }
    

(http://guides.rubyonrails.org/layouts_and_rendering.html#passing-local-variables)

http://guides.rubyonrails.org/layouts_and_rendering.html#passing-local-variables

yield& content_for

yield& content_for

  • yieldis typically used in layouts. It tells Rails to put the content for this block at thatplace in the layout.
  • When you do yield :somethingassociated with content_for :something, you can pass a block of code (view) to display where the yield :somethingis placed (see example below).
  • yield通常用于布局。它告诉 Rails 将这个块的内容放在布局中的那个位置。
  • 当您yield :something与 关联时content_for :something,您可以传递一个代码块(视图)来显示yield :something放置的位置(参见下面的示例)。


A small example about yield:

关于产量的一个小例子:

In your layout:

在您的布局中:

<html>
<head>
 <%= yield :html_head %>
</head>
<body>
 <div id="sidebar">
   <%= yield :sidebar %>
 </div>
</body>

In one of your view:

在您的观点之一:

<% content_for :sidebar do %>
  This content will show up in the sidebar section
<% end %>

<% content_for :html_head do %>
  <script type="text/javascript">
    console.log("Hello World!");
  </script>
<% end %>

This will produce the following HTML:

这将产生以下 HTML:

<html>
<head>
  <script type="text/javascript">
    console.log("Hello World!");
  </script>
</head>
<body>
 <div id="sidebar">
   This content will show up in the sidebar section
 </div>
</body>


Posts that might help:

可能有帮助的帖子

Links to documentation & guides:

文档和指南的链接

回答by Saumya Mehta

About render , render :partial and yield

关于 render 、 render :partial 和 yield

  • render :template and render :partial are two files in rails..


    render :template are mostly created according to an action with syntax demo.html.erb

    render :partial are reuseable and called from different views , are shared among many pages in application and syntax is _demo.html.erb

  • yield and render..


  • render :template 和 render :partial 是 rails 中的两个文件。


    render :template 主要是根据使用语法 demo.html.erb 的动作创建的

    render :partial 可重用并从不同的视图调用,在应用程序中的许多页面之间共享,语法为 _demo.html.erb

  • 产量和渲染..


Yield is a way to call a block of code with its output but render will include a partial page template where it is called. In rails yield is mostly used in layout whereas render is used in actions or their templates

Yield 是一种使用其输出调用代码块的方法,但 render 将包含调用它的部分页面模板。在 rails 中,yield 主要用于布局,而 render 用于动作或其模板

回答by Subhash Chandra

Some developers think of redirect_to as a sort of goto command, moving execution from one place to another in your Rails code. This is not correct. Your code stops running and waits for a new request for the browser. It just happens that you've told the browser what request it should make next, by sending back an HTTP 302 status code.

一些开发人员认为 redirect_to 是一种 goto 命令,在 Rails 代码中将执行从一个地方移动到另一个地方。这是不正确的。您的代码停止运行并等待浏览器的新请求。碰巧您已经通过发回 HTTP 302 状态代码告诉浏览器它接下来应该发出什么请求。

Consider these actions to see the difference:

考虑这些操作以查看差异:

def index
  @books = Book.all
end

def show
  @book = Book.find_by(id: params[:id])
  if @book.nil?
    render action: "index"
  end
end

With the code in this form, there will likely be a problem if the @bookvariable is nil. Remember, a render :actiondoesn't run any code in the target action, so nothing will set up the @books variable that the index view will probably require. One way to fix this is to redirect instead of rendering:

使用这种形式的代码,如果@book变量为零,则可能会出现问题。请记住, arender :action不会在目标操作中运行任何代码,因此不会设置索引视图可能需要的 @books 变量。解决此问题的一种方法是重定向而不是渲染:

def index
  @books = Book.all
end

def show
  @book = Book.find_by(id: params[:id])
  if @book.nil?
    redirect_to action: :index
  end
end

With this code, the browser will make a fresh request for the index page, the code in the index method will run, and all will be well.

有了这段代码,浏览器会重新请求索引页,索引方法中的代码就会运行,一切都会好起来的。

The only downside to this code is that it requires a round trip to the browser: the browser requested the show action with /books/1 and the controller finds that there are no books, so the controller sends out a 302 redirect response to the browser telling it to go to /books/, the browser complies and sends a new request back to the controller asking now for the index action, the controller then gets all the books in the database and renders the index template, sending it back down to the browser which then shows it on your screen.

这段代码唯一的缺点是它需要到浏览器来回:浏览器用 /books/1 请求 show 动作,控制器发现没有书,所以控制器向浏览器发送 302 重定向响应告诉它去/books/,浏览器遵守并发送给控制器一个新的请求,现在请求索引操作,控制器然后获取数据库中的所有书籍并呈现索引模板,将其发送回浏览器,然后将其显示在您的屏幕上。

While in a small application, this added latency might not be a problem, it is something to think about if response time is a concern. We can demonstrate one way to handle this with a contrived example:

虽然在小型应用程序中,这种增加的延迟可能不是问题,但如果响应时间是一个问题,则需要考虑。我们可以用一个人为的例子来演示处理这个问题的一种方法:

def index
  @books = Book.all
end

def show
  @book = Book.find_by(id: params[:id])
  if @book.nil?
    @books = Book.all
    flash.now[:alert] = "Your book was not found"
    render "index"
  end
end

This would detect that there are no books with the specified ID, populate the @books instance variable with all the books in the model, and then directly render the index.html.erb template, returning it to the browser with a flash alert message to tell the user what happened.

这将检测到没有具有指定 ID 的书籍,将模型中的所有书籍填充到 @books 实例变量中,然后直接渲染 index.html.erb 模板,将其返回给浏览器并带有一个闪光警报消息告诉用户发生了什么。