Ruby On Rails : Views : Fact : ‘Counter’ in Rails partials

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 %>

With this change, you can access an instance of the @products collection as the item local variable within the partial.

You can also pass in arbitrary local variables to any partial you are rendering with the locals: {}option:

<%= render partial: "product", collection: @products,
           as: :item, locals: {title: "Products Page"} %>

In this case, the partial will have access to a local variable title with the value “Products Page”.

Rails also makes a counter variable available within a partial called by the collection, named after the member of the collection followed by _counter. For example, if you’re rendering @products, within the partial you can refer to product_counter to tell you how many times the partial has been rendered. This does not work in conjunction with the as: :value option.

Rails: Save Markups in variables for later use

content_for(name, content = nil, options = {}, &block) public

Calling content_for stores a block of markup in an identifier for later use. In order to access this stored content in other templates, helper modules or the layout, you would pass the identifier as an argument tocontent_for.

Note: yield can still be used to retrieve the stored content, but calling yield doesn’t work in helper modules, while content_for does.

<% content_for :not_authorized do %>
  alert('You are not authorized to do that!')
<% end %>

You can then use content_for :not_authorized anywhere in your templates.

<%= content_for :not_authorized if current_user.nil? %>

This is equivalent to:

<%= yield :not_authorized if current_user.nil? %>

content_for, however, can also be used in helper modules.

module StorageHelper
  def stored_content
    content_for(:storage) || "Your storage is empty"

This helper works just like normal helpers.

<%= stored_content %>

You can also use the yield syntax alongside an existing call to yield in a layout. For example:

<%# This is the layout %>
<html xmlns="" xml:lang="en" lang="en">
  <title>My Website</title>
  <%= yield :script %>
  <%= yield %>

And now, we’ll create a view that has a content_for call that creates the script identifier.

<%# This is our view %>
Please login!

<% content_for :script do %>
  <script>alert('You are not authorized to view this page!')</script>
<% end %>

Then, in another view, you could to do something like this:

<%= link_to 'Logout', action: 'logout', remote: true %>

<% content_for :script do %>
  <%= javascript_include_tag :defaults %>
<% end %>

That will place script tags for your default set of JavaScript files on the page; this technique is useful if you’ll only be using these scripts in a few views.

Note that content_for concatenates (default) the blocks it is given for a particular identifier in order. For example:

 <% content_for :navigation do %>
   <li><%= link_to 'Home', action: 'index' %></li>
 <% end %>

And in other place:

 <% content_for :navigation do %>
   <li><%= link_to 'Login', action: 'login' %></li>
 <% end %>

Then, in another template or layout, this code would render both links in order:

<ul><%= content_for :navigation %></ul>

If the flush parameter is true content_for replaces the blocks it is given. For example:

<% content_for :navigation do %>
  <li><%= link_to 'Home', action: 'index' %></li>
<% end %>

<%#  Add some other content, or use a different template: %>

<% content_for :navigation, flush: true do %>
  <li><%= link_to 'Login', action: 'login' %></li>
<% end %>

Then, in another template or layout, this code would render only the last link:

<ul><%= content_for :navigation %></ul>

Lastly, simple content can be passed as a parameter:

<% content_for :script, javascript_include_tag(:defaults) %>

WARNING: content_for is ignored in caches. So you shouldn’t use it for elements that will be fragment cached.

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

    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
    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)
        render "edit"
       # render :edit you can rather use symbol

    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"
    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 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) 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 The :location Option

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

render xml: photo, location: photo_url(photo) 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

<%= stylesheet_link_tag "" %>

Rails: Render partials via ajax without having to load views

Normally, we render partials via views by using render [parial_without_underscore]. But we can also render partials in our document using ajax. To do this we need to

  1. Create an action method that we would call to load the partial and write the following code inside the action method


    *layout: false will disable the loading of layouts and headers we create

    *partial: “popup_form” will load the parial “_popup_form.html.erb” file


    *@author Shiva Bhusal


    respond_to do |format|
    format.html { render partial: "popup_form", layout: false }

  2. Now make an ajax call to url controller/action/
  3. Grab the data via HTTP response and render in the webpage