Q:

Trying to load a view in application layout and calling its action on every page

I am trying to load a view with a list of Product Type. I need the view to be displayed on every page. But the action is not getting called in other pages, please tell me where I'm going wrong and suggest any alternatives.
index.html.erb
<h1>Listing the types of products</h1>
<% content_for :sidebar do %>
<% @types.each do |type| %>             
    <li><%= link_to type.name, product_type_path(type)%>    <!-- go to product_type controller show action-->
    <%= link_to "Edit", edit_product_type_path(type) %> <!-- go to product_type controller edit action -->
    <%= link_to "Delete", product_type_path(type) , method: :delete, data: { confirm: "Are U Sure?" }%>     <!-- go to product_type controller delete action and a pop to confirm the action -->
    </li>
<% end %>

<h3><%= link_to "New Type Of Product", new_product_type_path %></h3>
<h3><%= link_to "All Types of Products", products_path %></h3> <!-- All types of products listed for admin's ease -->
<% end %>

This is the Application layout I'm using.
Application.html.erb
<!DOCTYPE html>
<html>
  <head>
    <%= render 'layouts/title' %>
    <%= render 'layouts/rails_defaults'%>
    <%= render 'layouts/shim' %>
  </head>

  <body>
    <aside><%= yield: sidebar %></aside>
    <%= render 'layouts/header' %>
    <%= render 'layouts/flash' %>
      <div class="container">
        <%= yield %>
      </div>
    <%= render 'layouts/footer' %>
  </body>
</html>

As you can see I'm using yield: sidebar , but its not working properly, in the sense the action index is not getting called if I go to different page.
Product_types Controller
class ProductTypesController < ApplicationController
    def index
        @types = ProductType.all        #to get all the records to an instance variable(array in the case) lasts only until the scope lasts. Since the is defined in index, it lasts only till you are using the index view
    end

    def new
        @type = ProductType.new         #to create a new record into the respective model
    end

    def show
        @type = ProductType.find(params[:id])       #Finding the type of product click on
        @products = Product.where(value: @type.value)       #finding all the products whose value field is same as the type of product value(primary key) 
    end

    def create
        @type = ProductType.new(type_params)        #type params defined below in private class 
        if @type.save                               #save the product created
            redirect_to root_url                    #and redirect to root
        else                                        #if doesnt save and error occurs
            render 'new'                            #error occurs and render 'new' view
        end
    end

    def edit
        @type = ProductType.find(params[:id])
    end

    def update
        @type = ProductType.find(params[:id])
        if @type.update(type_params)                #update the params
            redirect_to root_url                    #if updated redirect to root
        else                        
            render 'edit'                           #else if error occurs render 'edit' view again
        end
    end

    def destroy
        ProductType.find(params[:id]).destroy       #destroy the record
        redirect_to root_url                        #redirect to root 
    end

    private

        def type_params
            params.require(:product_type).permit(:name,:value)      #used to only permit type and value thorugh request(to prevent hacking) used in create and update action above
        end
end

The action is not getting called whenever i jump to another page.
please suggest alternatives.

A:

content_for / yield are for the current view only invoked by just the current action. So if the other pages dont have a content_for :sidebar in their views, then they wont have a sidebar. And if you just include in the content_for directly, it still wont execute any extra controller logic required.
Use a helper or partial like your layouts/header if you want resuable content, rather than a reusable "space".
For things you dont want to do only in a partial, but also not only in direct Ruby (with content_tag, etc.), you might combine a partial and a helper.
class ApplicationHelper # Or any other helper
  def products_sidebar
    products = Product.where(show_on_sidebar: true).order(:popularity) # Or whatever you like
    render partial: "shared/products_sidebar", locals: {products: products}
  end
end

shared/_products_sidebar.html.erb (Id definitely give consideration to other template engines)
<div id="products_sidebar">
    <% products.each do |product| %>
        <div><%=product.name%></div> <!--whatever you want it to look like-->
    <% end %>
</div>

Then in your index, you can just call it, and it wont depend on the action/controller currently being processed.
<body>
    <aside><%= products_sidebar %></aside>

