{"id":3917,"date":"2015-05-04T20:31:23","date_gmt":"2015-05-05T00:31:23","guid":{"rendered":"http:\/\/webninjataylor.com\/library\/?p=3917"},"modified":"2015-05-04T21:22:09","modified_gmt":"2015-05-05T01:22:09","slug":"ruby-on-rails-layouts-and-rendering","status":"publish","type":"post","link":"https:\/\/webninjataylor.com\/library\/ruby-on-rails-layouts-and-rendering\/","title":{"rendered":"Ruby on Rails: Layouts and Rendering"},"content":{"rendered":"<ul>\n<li>By default, controllers in Rails automatically render views with names that correspond to valid routes<\/li>\n<li>The rule is that if you do not explicitly render something at the end of a controller action, Rails will automatically look for the action_name.html.erb template in the controller&#8217;s view path and render it\n<ul>\n<li>If you want to render the view that corresponds to a different template within the same controller, you can use render with the name of the view\n<pre>def update\r\n @book = Book.find(params[:id])\r\n if @book.update(book_params)\r\n   redirect_to(@book)\r\n else\r\n   render \"edit\"   # you can also use a symbol instead of a string  ... render :edit\r\n end\r\nend<\/pre>\n<\/li>\n<li>You can render a template from an entirely different controller from the one that contains the action code (relative to app\/views)&#8230;\n<pre>render \"products\/show\"<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>You can render JSOn with\u00a0<span class=\"code\">render json: @product<\/span><\/li>\n<li>Use the layout option\u00a0to tell Rails to use a specific file as the layout for the current action:\n<pre>render layout: \"special_layout\"<\/pre>\n<\/li>\n<li>There are six asset tag helpers in Rails&#8230;\n<ul>\n<li>auto_discovery_link_tag (for RSS feeds)<\/li>\n<li>javascript_include_tag<\/li>\n<li>stylesheet_link_tag<\/li>\n<li>image_tag &#8230; e.g.<span class=\"code\">&lt;%= image_tag &#8220;home.gif&#8221;, alt: &#8220;Go Home&#8221;,\u00a0id: &#8220;HomeImage&#8221;,\u00a0class: &#8220;nav_bar&#8221; %&gt;<\/span><\/li>\n<li>video_tag<\/li>\n<li>audio_tag<\/li>\n<\/ul>\n<\/li>\n<li><strong>Yield<\/strong>\n<ul>\n<li>Within the context of a layout, <span class=\"code\">yield<\/span> identifies a section where content from the view should be inserted\n<pre>\/\/ Layout ...\r\n&lt;html&gt;\r\n &lt;head&gt;\r\n   &lt;%= yield :head %&gt;\r\n &lt;\/head&gt;\r\n &lt;body&gt;\r\n   &lt;%= yield %&gt;\r\n &lt;\/body&gt;\r\n&lt;\/html&gt;\r\n\r\n\/\/ View ...\r\n&lt;% content_for :head do %&gt;\r\n &lt;title&gt;A simple page&lt;\/title&gt;\r\n&lt;% end %&gt;\r\n \r\n&lt;p&gt;Hello, Rails!&lt;\/p&gt;<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li><strong>Partials<\/strong>\n<ul>\n<li>To render a partial as part of a view, you use the render method within the view\n<pre>\/\/ Render a file named _menu.html.erb at that point within the view being rendered\r\n&lt;%= render \"menu\" %&gt;\r\n\r\n\/\/ ... or ...\r\n&lt;%= render \"shared\/ad_banner\" %&gt;\r\n\r\n\/\/ ... or a partial using its own layout ...\r\n&lt;%= render partial: \"link_area\", layout: \"graybar\" %&gt;<\/pre>\n<ul>\n<li>This would look for a partial named _link_area.html.erb and render it using the layout _graybar.html.erb<\/li>\n<li>Note that layouts for partials follow the same leading-underscore naming as regular partials, and are placed in the same folder with the partial that they belong to (not in the master layouts folder)<\/li>\n<\/ul>\n<\/li>\n<li>You can pass-in local variables to make the partials even more powerful\/customized &#8230;\n<pre>\/\/ new.html.erb\r\n&lt;%= render partial: \"form\", locals: {zone: @zone} %&gt;\r\n\/\/ ... or ...\r\n&lt;%= render partial: \"customer\", object: @new_customer %&gt;\r\n\/\/ ... or if you have an instance of a model to render in the partial use the shorthand ...\r\n&lt;%= render @customer %&gt;\r\n\/\/ ... or render partials once for each object in a collection ...\r\n&lt;%= render partial: \"product\", collection: @products %&gt;\r\n\/\/ ... or with a custom local variable\r\n&lt;%= render partial: \"product\", collection: @products, as: :item %&gt;\r\n\r\n\/\/ _form.html.erb\r\n&lt;%= form_for(zone) do |f| %&gt;\r\n &lt;p&gt;\r\n   &lt;b&gt;Zone name&lt;\/b&gt;&lt;br&gt;\r\n   &lt;%= f.text_field :name %&gt;\r\n &lt;\/p&gt;\r\n &lt;p&gt;\r\n   &lt;%= f.submit %&gt;\r\n &lt;\/p&gt;\r\n&lt;% end %&gt;<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>Resources<\/h2>\n<ul>\n<li><a href=\"http:\/\/guides.rubyonrails.org\/layouts_and_rendering.html\" target=\"_blank\">Layouts and Rendering in Rails<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>By default, controllers in Rails automatically render views with names that correspond to valid routes The rule is that if you do not explicitly render something at the end of a controller action, Rails will automatically look for the action_name.html.erb template in the controller&#8217;s view path and render it If you want to render the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[144],"tags":[152],"class_list":["post-3917","post","type-post","status-publish","format-standard","hentry","category-web-shots","tag-ruby"],"_links":{"self":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/3917","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/comments?post=3917"}],"version-history":[{"count":7,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/3917\/revisions"}],"predecessor-version":[{"id":3924,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/3917\/revisions\/3924"}],"wp:attachment":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/media?parent=3917"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/categories?post=3917"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/tags?post=3917"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}