What is the difference between render and yield in Rails

前端 未结 3 728
自闭症患者
自闭症患者 2021-02-13 11:00

Can someone explain the difference between \"<%= render %>\" and \"<%= yield %> with <% content_for :partial do %>/

3条回答
  •  时光取名叫无心
    2021-02-13 11:31

    When your controller method exits, it renders the associated file. So the edit controller renders edit.html.erb. It uses the specified layout or application.html.erb if none is specified.

    Within your layout file, when you call yield it will fill in the information from your render. If you call yield with a parameter, it will look for a content_for section in your render file matching that parameter. I'm not completely sure, but I don't think you can call yield from outside of your layout file, and I don't think it will fill in any information except that found in your render file.

    Anywhere in your layout file or your rendered file, you can render a partial by calling render with the partial name minus the underscore.

    I hope that helps.

    Edit to answer question in comment:

    yield and render perform similar functions however yield only looks in the render file whereas render specifies which file to render. Also, render outputs the entire file, but yield with a parameter can output just a subsection of the file.

提交回复
热议问题