Rails : Layout and rendering :: Gist/Theme

2 Creating Responses

From the controller’s point of view, there are three ways to create an HTTP 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

    2.1 Rendering by Default: Convention Over Configuration in Action

    if you have this code in your BooksController class:

    class BooksController < ApplicationController
    end

    And the following in your routes file:

    resources :books

    And you have a view file app/views/books/index.html.erb:

    <h1>Books are coming soon!</h1>

    Rails will automatically render app/views/books/index.html.erb when you navigate to /books and you will see “Books are coming soon!” on your screen.

  • 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’s view path and render it. So in this case, Rails will render the app/views/books/index.html.erb file
    2.2.1 Rendering Nothing

    Perhaps the simplest thing you can do with render is to render nothing at all:

    render nothing: true
    or
    head :bad_request
    2.2.2 Rendering an Action’s View

    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:

    def update
      @book = Book.find(params[:id])
      if @book.update(book_params)
        redirect_to(@book)
      else
        render "edit"
       # render :edit you can rather use symbol
      end
    end

    If the call to update fails, calling the update action in this controller will render the edit.html.erbtemplate belonging to the same controller.

    2.2.3 Rendering an Action’s Template from Another Controller
    you can render the results of an action to a template in app/views/products this way:
    render "products/show"

    you can use the :template option (which was required on Rails 2.2 and earlier):

    render template: "products/show"
    2.2.4 Rendering an Arbitrary File
    render "/u/apps/warehouse_app/current/app/views/products/show"
    or
    render file: "/u/apps/warehouse_app/current/app/views/products/show"
    2.2.7 Rendering Text

    You can send plain text – with no markup at all – back to the browser by using the :plain option to render:

    render plain: "OK"

    Rendering pure text is most useful when you’re responding to Ajax or web service requests that are expecting something other than proper HTML.

    2.2.9 Rendering JSON

    JSON is a JavaScript data format used by many Ajax libraries. Rails has built-in support for converting objects to JSON and rendering that JSON back to the browser:

    render json: @product
    2.2.10 Rendering XML

    Rails also has built-in support for converting objects to XML and rendering that XML back to the caller:

    render xml: @product
    2.2.11 Rendering Vanilla JavaScript

    Rails can render vanilla JavaScript:

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

    This will send the supplied string to the browser with a MIME type of text/javascript.

    2.2.13 Options for render

    Calls to the render method generally accept four options:

    • :content_type
    • :layout
    • :location
    • :status

2.2.13.1 The :content_type Option

render file: filename, content_type: "application/rss"
By default, Rails will serve the results of a rendering operation with the MIME content-type of text/html (or application/json if you use the :json option)
2.2.13.2 The :layout Option
You can use the :layout option to tell Rails to use a specific file as the layout for the current action:
render layout: "special_layout"

You can also tell Rails to render with no layout at all:

render layout: false
2.2.13.3 The :location Option

You can use the :location option to set the HTTP Location header:

render xml: photo, location: photo_url(photo)
2.2.13.4 The :status Option

Rails will automatically generate a response with the correct HTTP status code (in most cases, this is 200 OK). You can use the :status option to change this:

render status: 500
render status: :forbidden

To include app/assets/stylesheets/main.css and app/assets/stylesheets/columns.css:

<%= stylesheet_link_tag "main", "columns" %>

To include app/assets/stylesheets/main.css and app/assets/stylesheets/photos/columns.css:

<%= stylesheet_link_tag "main", "photos/columns" %>

To include http://example.com/main.css:

<%= stylesheet_link_tag "http://example.com/main.css" %>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s