title: How to link to a nested route action inside a parent loop?
body: <p>I'm trying to display everything on my index page, but when I try to link to a new_story_substory_path I get undefined local variable or method substory. </p>
  
  <p>Here is the code:</p>
  
  <p><strong>index.html.erb</strong></p>
  
  <pre><code>&lt;% @stories.each do |story| %&gt;
    &lt;h3&gt;&lt;p&gt;&lt;%= story.title %&gt;&lt;/p&gt;&lt;/h3&gt;
    &lt;p&gt;&lt;%= story.plot %&gt;&lt;/p&gt;
  
    &lt;% if story.user == current_user %&gt;
      &lt;%= link_to 'Show XXX', story_path(story), class: "btn btn-success" %&gt;
      &lt;%= link_to 'Edit', edit_story_path(story), class: "btn btn-success" %&gt;
      &lt;%= link_to "Delete", story_path(story), method: :delete, data: { confirm: "Are you sure?" }, class: "btn btn-success" %&gt;&lt;/br&gt;
    &lt;% end %&gt;
  
    &lt;% story.substories.each do |substory| %&gt;
      &lt;h4&gt;&lt;p&gt;&lt;%= substory.title %&gt;&lt;/p&gt;&lt;/h3&gt;
      &lt;p&gt;&lt;%= substory.subplot %&gt;&lt;/p&gt;
  
      &lt;% if story.user == current_user %&gt;
        &lt;%= link_to 'Show', story_substory_path(substory.story, substory), class: "btn btn-default" %&gt;
        &lt;%= link_to 'Edit', edit_story_substory_path(substory.story, substory), class: "btn btn-default" %&gt;
        &lt;%= link_to "Delete", story_substory_path(substory.story, substory), method: :delete, data: { confirm: "Are you sure?" }, class: "btn btn-default" %&gt;
      &lt;% end %&gt;
    &lt;% end %&gt;
    &lt;br&gt;
  
    &lt;% if story.user == current_user %&gt;
      &lt;br&gt;
      &lt;br&gt;
      # this is where I'm getting the error:
      &lt;%= link_to 'New subplot', new_story_substory_path(substory.story, substory), class: "btn btn-warning" %&gt;
      # however if I move this up, inside the second loop, it works perfectly.
      &lt;br&gt;
    &lt;% end %&gt;
  &lt;% end %&gt;
  
  &lt;br&gt;
  
  &lt;%= link_to 'New Story', new_story_path, class: "btn btn-danger" %&gt;
  </code></pre>
  
  <p>Here is my controller, routes and models:</p>
  
  <pre><code>class StoriesController &lt; ApplicationController
    before_action :set_story, only: [:show, :edit, :update, :destroy]
    before_action :authenticate_user!, except: [:index, :show]
  
    def index
      @stories = Story.all
    end
  
    def show
    end
  
    def new
      @story = current_user.stories.build
    end
  
    def edit
    end
  
    def create
      @story = current_user.stories.build(story_params)
  
      respond_to do |format|
        if @story.save
          format.html { redirect_to root_path, notice: 'Story was successfully created.' }
          format.json { render :show, status: :created, location: root_path }
        else
          format.html { render :new }
          format.json { render json: root_path.errors, status: :unprocessable_entity }
        end
      end
    end
  
    def update
      respond_to do |format|
        if @story.update(story_params)
          format.html { redirect_to root_path, notice: 'Story was successfully updated.' }
          format.json { render :show, status: :ok, location: root_path }
        else
          format.html { render :edit }
          format.json { render json: @story.errors, status: :unprocessable_entity }
        end
      end
    end
  
    def destroy
      @story.destroy
      respond_to do |format|
        format.html { redirect_to stories_url, notice: 'Story was successfully destroyed.' }
        format.json { head :no_content }
      end
    end
  
    private
      def set_story
        @story = Story.find(params[:id])
      end
  
      def story_params
        params.require(:story).permit(:title, :plot)
      end
  end
  </code></pre>
  
  <hr>
  
  <pre><code>Rails.application.routes.draw do
  
    devise_for :users
  
    resources :stories do
      resources :substories
    end
  
    root 'stories#index'
  
  end
  </code></pre>
  
  <hr>
  
  <pre><code>class Story &lt; ActiveRecord::Base
      has_many :substories, dependent: :destroy
      belongs_to :user
  end
  
  class Substory &lt; ActiveRecord::Base
      belongs_to :story
      belongs_to :user
  end
  
  class User &lt; ActiveRecord::Base
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :trackable, :validatable
  
    has_many :stories, dependent: :destroy
    has_many :substories, dependent: :destroy
  end
  </code></pre>
  
  <hr>
  
  <p>what am I missing?</p>
  
  <p>Edit:</p>
  
  <p>Here is the controller for the substories:</p>
  
  <pre><code>class SubstoriesController &lt; ApplicationController
    before_action :set_substory, only: [:show, :edit, :update, :destroy]
    before_action :authenticate_user!
    before_action :set_story
  
    def index
      @Substories = @story.substories.all
    end
  
    def show
    end
  
    def new
      @substory = Substory.new
    end
  
    def edit
    end
  
    def create
      @substory = Substory.new(substory_params)
      @substory.user_id = current_user.id
      @substory.story_id = @story.id
      if
        @substory.save
          redirect_to @story
      else
        render 'new'
      end
    end
  
    def update
      respond_to do |format|
        if @substory.update(substory_params)
          format.html { redirect_to root_path, notice: 'Story was successfully updated.' }
          format.json { render :show, status: :ok, location: root_path }
        else
          format.html { render :edit }
          format.json { render json: @story.errors, status: :unprocessable_entity }
        end
      end
    end
  
    def destroy
      @substory.destroy
      redirect_to root_path
    end
  
    private
      def set_story
        @story = Story.find(params[:story_id])
      end
  
      def set_substory
        @substory = Substory.find(params[:id])
      end
  
      def substory_params
        params.require(:substory).permit(:title, :subplot)
      end
  end
  </code></pre>