2

I would like to capture the parent Id param (foreign key) before a new child record is saved.

I have the two setup as nested resources in the routes file and the link to the new child controller action shows the parent ID nested correctly.

When I enter the data for the child and click "Create" the record is created however I cannot capture the ID of the parent.

Edit

I am using a nested sets model to relate the table back to itself so there is no FK column. Because there is no FK I need to capture the parent ID before the record is saved and update the other record's lft & rgt fields accordingly.

http://www.ibase.ru/devinfo/DBMSTrees/sqltrees.html

Routes:

Rails.application.routes.draw do
  resources :regions do
    resources :regions
  end
End

New Form

The region list includes the following link to create a new child region:

stackoverflow will not allow me to post the form field however I think my problem lies in that the form is posting to @region which is from the scaffold before I nested the resources.

rake routes displays the following:

              Prefix Verb   URI Pattern                                    Controller#Action
      region_regions GET    /regions/:region_id/regions(.:format)          regions#index
                     POST   /regions/:region_id/regions(.:format)          regions#create
   new_region_region GET    /regions/:region_id/regions/new(.:format)      regions#new
  edit_region_region GET    /regions/:region_id/regions/:id/edit(.:format) regions#edit
       region_region GET    /regions/:region_id/regions/:id(.:format)      regions#show
                     PATCH  /regions/:region_id/regions/:id(.:format)      regions#update
                     PUT    /regions/:region_id/regions/:id(.:format)      regions#update
                     DELETE /regions/:region_id/regions/:id(.:format)      regions#destroy
             regions GET    /regions(.:format)                             regions#index
                     POST   /regions(.:format)                             regions#create
          new_region GET    /regions/new(.:format)                         regions#new
         edit_region GET    /regions/:id/edit(.:format)                    regions#edit
              region GET    /regions/:id(.:format)                         regions#show
                     PATCH  /regions/:id(.:format)                         regions#update
                     PUT    /regions/:id(.:format)                         regions#update
                     DELETE /regions/:id(.:format)                         regions#destroy


<%= form_for(@region) do |f| %>
...
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
Dercni
  • 1,216
  • 3
  • 18
  • 38

1 Answers1

1

You should be using params[:region_id] in your RegionsController action, this should get the parentId in ur route.

As it is the same actions for both routes you should also be doing some checks to know where are you in the scope.

So I would suggest something like

class RegionController < ApplicationController def create if params[:region_id] parent = Region.find(params[:region_id]) # ... end ... end end

gbrennon
  • 899
  • 2
  • 11
  • 30