title: Rails Associations - Creating a new record through a form with belongs_to
body: <p>I have a Course model:</p>
  <pre><code>class Course &lt; ApplicationRecord
    has_many :sub_courses
  
    validates :title, presence: true
  
    # Course associated to SubCourse via 'sub_course_id' on Course table
  end
  </code></pre>
  <p>And a SubCourse model:</p>
  <pre><code>class SubCourse &lt; ApplicationRecord
    belongs_to :course
  
    # SubCourse associated to Course via 'course_id' on SubCourse table
  end
  
  </code></pre>
  <p>On the <code>courses.show.html</code> (specific course page e.g. admin/courses/1) I have a button that links to a new sub course page</p>
  <pre><code>%table
    %tr
      %td= @course.title
      %td= @course.description
  
  = button_to &quot;Add New Sub Course&quot;, new_admin_sub_course_path(course_id: @course.id), method: :post
  </code></pre>
  <p>The new sub_course page <code>sub_courses.new.html</code> form.</p>
  <pre><code>= form_for @sub_course, url: admin_sub_courses_path do |f|
    = f.label :title
    = f.text_field :title
    = f.label :description
    = f.text_field :description
    = f.submit
  </code></pre>
  <p>When going to the sub course new page I see the error <code>No route matches [POST] &quot;/admin/sub_courses/new&quot;</code></p>
  <p>My <code>sub_course_controller.rb</code> looks like this:</p>
  <pre><code>def new
      @course = Course.find(params.require(:course_id))
      @sub_course = @course.sub_course.new
    end
  
    def create
      if @sub_course.save
        redirect_to admin_sub_courses_path, notice: &quot;saved&quot;
      else
        render &quot;new&quot;
      end
    end
  </code></pre>
  <p>And my routes looks like this:</p>
  <pre><code>namespace :admin do
      resources :courses, { :only =&gt; [:index, :new, :create, :edit, :destroy, :update, :show] }
      resources :sub_courses 
    end
  </code></pre>
  <p>How do I successfully create a sub_course thats automatically associated with its course from the original show page I came from?</p>
  <p>Schema structure looks like this:</p>
  <pre><code>  create_table &quot;courses&quot;, force: :cascade do |t|
      t.datetime &quot;created_at&quot;, null: false
      t.datetime &quot;updated_at&quot;, null: false
      t.string &quot;title&quot;
      t.string &quot;description&quot;
      t.integer &quot;sub_course_id&quot;
    end
  
    create_table &quot;sub_courses&quot;, force: :cascade do |t|
      t.string &quot;title&quot;
      t.text &quot;description&quot;
      t.string &quot;question&quot;
      t.string &quot;possible_answer&quot;
      t.string &quot;correct_answer&quot;
      t.datetime &quot;created_at&quot;, null: false
      t.datetime &quot;updated_at&quot;, null: false
      t.integer &quot;course_id&quot;
    end
  </code></pre>
  <p>After running rake routes for sub courses:</p>
  <pre><code>admin_sub_courses GET    /admin/sub_courses(.:format)                                                             admin/sub_courses#index
                            POST   /admin/sub_courses(.:format)                                                             admin/sub_courses#create
       new_admin_sub_course GET    /admin/sub_courses/new(.:format)                                                         admin/sub_courses#new
      edit_admin_sub_course GET    /admin/sub_courses/:id/edit(.:format)                                                    admin/sub_courses#edit
           admin_sub_course GET    /admin/sub_courses/:id(.:format)                                                         admin/sub_courses#show
                            PATCH  /admin/sub_courses/:id(.:format)                                                         admin/sub_courses#update
                            PUT    /admin/sub_courses/:id(.:format)                                                         admin/sub_courses#update
                            DELETE /admin/sub_courses/:id(.:format)                                                         admin/sub_courses#destroy
  </code></pre>