Q:

Displaying items captured via a nested form

I'm trying to build a small expense tracking app. Using the nested_form gem to add line items. There is an Expense model which accepts nested attributes. Items belong to expenses and there is a foreign key association. Here's the expense controller:
class ExpensesController < ApplicationController
    def new
        @expense = Expense.new
        @item = @expense.items.build
    end

    def index
        @expenses = Expense.all
        #@items = Item.where(:expense_id => @expense.id)
    end

    def show
        @expense = Expense.find(params[:id])
        @items = Item.where(:expense_id => @expense.id)
    end

    def create
        @expense = Expense.new(expense_params)
        respond_to do |format|
            if @expense.save
                format.html { redirect_to @expense, notice: 'Expense Report Submitted.' }
                format.json { render :show, status: :created, location: @expense }
            else
                format.html { render :new }
                format.json { render json: @expense.errors, status: :unprocessable_entity }
            end
        end
    end

    def edit
        @expense = Expense.find(params[:id])
    end

    def update
        @expense = Expense.find(params[:id])
        if @expense.save(expense_params)
            flash[:notice] = "Expense Report Updated"
            redirect_to @expense
        else
            render 'edit'
        end
    end

    def destroy
        @expense = Expense.find(params[:id])
        @expense.destroy
        redirect_to 'root'
    end

    private
    def expense_params
        params.require(:expense).permit(:department_id, :expense_type_id, :notes, items_attributes: [:id, :description, :amount, :issue_date, :_destroy])
    end
end

The form looks like:
<%= nested_form_for (@expense) do |f| %>    
    <% if @expense.errors.any? %>
      <div id="error_explanation">
        <h2><%= pluralize(@expense.errors.count, "error") %> prohibited
          this expense from being saved:</h2>
        <ul>
        <% @expense.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
        </ul>
      </div>
    <% end %>
    <div class"row">
        <div class="col-md-8">
            <div class="form-group">
                <%= f.label :department_id %><br>
                <%= f.collection_select(:department_id, Department.all, :id, :department_name, prompt: true, class: "dropdown-menu") %>
            </div>
            <div class="form-group">
                <%= f.label :expense_type_id %><br>
                <%= f.collection_select(:expense_type_id, ExpenseType.all, :id, :expense_name, prompt: true, class: "form-control")  %>
            </div>
            <%= f.fields_for :items do |i| %>
                <div class="form-group">
                    <%= i.label :description%>
                    <%= i.text_field :description, class: "form-control" %>
                </div>
                <div class="form-group">
                    <%= i.label :amount%>
                    <%= i.text_field :amount, class: "form-control" %>
                </div>
                <div class="form-group">
                    <%= i.label :issue_date%>
                    <%= i.date_select :issue_date, class: "form-control" %>
                </div>
                <%= i.link_to_remove "Remove", class: "btn btn-default" %>
            <% end %>
            <div><p><%= f.link_to_add "Add Expense", :items, class: "btn btn-default" %></p></div>
            <div class="form-group">
                <%= f.label :notes %>
              <%= f.text_area :notes, class: "form-control" %>
            </div>
            <%= f.submit "Submit", class: "btn btn-primary" %>
          <% end %>
        </div>
    </div>

When I hit submit, the expense and items are saved. I checked with Sqlite browser and the foreign key values are captured for each item. 
The index file looks like:
<% @expenses.each do |expense| %>
              <tr>
                  <td><%= expense.item.description %></td>
                  <td><%= number_to_currency(expense.item.amount) %></td>
                  <td><%= expense.item.issue_date %></td>
<% end %>

I tried the usual combinations (like expense.item.description) by passing through a block, but they aren't working. I would like to know how to display the expense and the associated items in the show and index pages.   

A:

Looking at your new action, in your models you would have
class Expense < ActiveRecord::Base
  has_many :items

  accepts_nested_attributes_for :items
end 

So when you are doing expense.items in your view it gives you an active record relation(since expense has many items), try this:
<% @expenses.each do |expense| %>
  <% expense.items.each do |item| %>
    <tr>
      <td><%= item.description %></td>
      <td><%= number_to_currency(item.amount) %></td>
      <td><%= item.issue_date %></td>
    </tr>
  <% end %>
<% end %>

