31
Layouts and Rendering in Rails by Farhan Faruque

Layouts and rendering in rails

Embed Size (px)

Citation preview

Page 1: Layouts and rendering in rails

Layouts and Rendering in Railsby Farhan Faruque

Page 2: Layouts and rendering in rails

contents

● overview● Creating Responses● Structuring Layouts

Page 3: Layouts and rendering in rails

Over-View!

Page 4: Layouts and rendering in rails

Creating Response

● Call render to create a full response to send back to the browser

● Call redirect_to to send an HTTP redirect status code to the browser

● Call head to create a response consisting solely of HTTP headers to send back to the browser

Page 5: Layouts and rendering in rails

Rendering by DefaultBy default, controllers in Rails automatically render views with names that correspond to valid routes.controller:class LocationController < ApplicationControllerendroutes:resources :locationsapp/views/location/index.html.erb:<h1>Bangladesh!</h1>navigate:/locations

Page 6: Layouts and rendering in rails

Using renderthe ActionController::Base#render method does the heavy lifting of rendering application's content for use by a browser.

● Rendering Nothingrender nothing: true

● Rendering an Action's Viewdef update

@book = Book.find(params[:id])

if @book.update(book_params)

redirect_to(@book)

else

render "edit"

end

end

Page 7: Layouts and rendering in rails

Using render

● Rendering an Action's Template from Another Controller

render "products/show"

● Rendering an Arbitrary File

render "/u/apps/warehouse_app/current/app/views/products/show"

● Using render with :inline

render inline: "<% products.each do |p| %><p><%= p.name%></p><% end %>"

Page 8: Layouts and rendering in rails

Using render

● Rendering Text

render plain: "OK"

● Rendering HTML

render html: "<strong>Not Found</strong>".html_safe

● Rendering JSON

render json: @product

● Rendering XML

render xml: @product

Page 9: Layouts and rendering in rails

Using render

● Rendering Vanilla JavaScript

render js: "alert('Hello Rails');"

● Rendering raw body

render body: "raw"

Page 10: Layouts and rendering in rails

Options for render● :content_type

render file: filename, content_type:"application/rss"

● :layoutrender layout: "special_layout"

● :location render xml: photo, location: photo_url(photo)

● :statusrender status: 500render status: :forbidden

Page 11: Layouts and rendering in rails

Finding LayoutsRails first looks for a file in app/views/layouts with the same base name as the controller

Controller Layout

Application layout

current layout

Page 12: Layouts and rendering in rails

Finding Layouts

● Specifying Layouts for Controllersclass ProductsController < ApplicationController

layout "inventory"#---

end

Page 13: Layouts and rendering in rails

Choosing Layouts at Runtime

class ProductsController < ApplicationController layout :products_layout def show @product = Product.find(params[:id]) end private def products_layout @current_user.special? ? "special" : "products" end end

Page 14: Layouts and rendering in rails

Conditional Layouts

class ProductsController < ApplicationController layout "product", except: [:index, :rss]end

class ProductsController < ApplicationController layout "product", only: [:show]end

Page 15: Layouts and rendering in rails

Using redirect_to

redirect_to method tells the browser to send a new request for a different URL.Example:

redirect_to photos_url

a special redirect that sends the user back to the page they just came from:

redirect_to :back

Page 16: Layouts and rendering in rails

The Difference Between render and redirect_to

render will render a particular view using the instance variables available in the action.

redirect_to will send a redirect to the user’s browser telling it to re-request a new URL. Then the browser will send a new request to that URL and it will go through the action for that URL

Page 17: Layouts and rendering in rails

Using head To Build Header-Only Responses

The head method can be used to send responses with only headers to the browser.

example:head :bad_requesthead :created, location: photo_path(@photo)

Page 18: Layouts and rendering in rails

Structuring Layouts

Within a layout,we have access to three tools for combining different bits of output to form the overall response:

● Asset tags● yield and content_for● Partials

Page 19: Layouts and rendering in rails

Asset Tag Helpers

● auto_discovery_link_tag● javascript_include_tag● stylesheet_link_tag● image_tag● video_tag● audio_tag

Page 20: Layouts and rendering in rails

Linking to Feeds with the auto_discovery_link_tag

<%= auto_discovery_link_tag(:rss, {action: "feed"}, {title: "RSS Feed"}) %>

Page 21: Layouts and rendering in rails

Linking to JavaScript Files with the javascript_include_tag

The javascript_include_tag helper returns an HTML script tag for each source provided.

<%= javascript_include_tag "main" %>

<script src='/assets/main.js'></script>

Page 22: Layouts and rendering in rails

Linking to CSS Files with the stylesheet_link_tag

The stylesheet_link_tag helper returns an HTML <link> tag for each source provided.

<%= stylesheet_link_tag "main" %>

<link rel="stylesheet" type="text/css" href="main.css">

Page 23: Layouts and rendering in rails

cont..

● Linking to Images with the image_tag<%= image_tag "header.png" %><%= image_tag "icons/delete.gif" %><%= image_tag "home.gif", alt: "Home" %>

● Linking to Videos with the video_tag<%= video_tag "movie.ogg" %><%= video_tag ["trailer.ogg", "movie.ogg"] %>

● Linking to Audio Files with the audio_tag<%= audio_tag "music.mp3" %>

Page 24: Layouts and rendering in rails

Understanding yield

Within the context of a layout, yield identifies a section where content from the view should be inserted. <html> <head> </head> <body> <%= yield %> </body></html>

Page 25: Layouts and rendering in rails

Using the content_for Method

The content_for method allows you to insert content into a named yield block in your layout.

<% content_for :head do %> <title>A simple page</title><% end %> <p>Hello, Rails!</p>

<html> <head> <title>A simple page</title> </head> <body> <p>Hello, Rails!</p> </body></html>

Page 26: Layouts and rendering in rails

Using Partials

A device for breaking the rendering process into more manageable chunks.

● Naming Partials

<%= render "menu" %>

Partials are named with a leading underscore to distinguish them from regular views, even though they are referred to without the underscore.

● Using Partials to Simplify Views<%= render "shared/ad_banner" %><h1>Products</h1><%= render "shared/footer" %>

Page 27: Layouts and rendering in rails

Partial Layouts

A partial can use its own layout file, just as a view can use a layout

<%= render partial: "link_area", layout: "graybar" %>

This would look for a partial named _link_area.html.erb and render it using the layout _graybar.html.erb

Page 28: Layouts and rendering in rails

Passing Local Variablesnew.html.erb

_form.html.erb

<h1>New zone</h1><%= render partial: "form", locals: {zone: @zone} %>

<%= form_for(zone) do |f| %> <p> <b>Zone name</b><br> <%= f.text_field :name %> </p> <p> <%= f.submit %> </p><% end %>

Page 29: Layouts and rendering in rails

Rendering Collections

Partials are very useful in rendering collections. When you pass a collection to a partial via the :collection option, the partial will be inserted once for each member in the collection:index.html.erb

_product.html.erb

<h1>Products</h1><%= render partial: "product", collection: @products %>

<p>Product Name: <%= product.name %></p>

Page 30: Layouts and rendering in rails

Local Variables

To use a custom local variable name within the partial, specify the :as option in the call to the partial:

<%= render partial: "product", collection: @products, as: :item %>

Page 31: Layouts and rendering in rails

Thank You