0

to avoid deep nesting routes I have done the following:

routes.rb

  resources :clients, controller: 'clients' do
    resources :sites, controller: 'clients/sites', except: [:index]
  end

  resources :sites, controller: 'clients/sites', except: [:index] do
    resources :site_contacts, controller: 'sites/site_contacts', except: [:index]
  end

My problem right now is after I Create a new Site Contact I Get an error that states

undefined method `site_contact_url' for #<Sites::SiteContactsController:0x007f9dcb980100>
Did you mean?  site_contact_params

my associations are all set up and correct but I am not sure how to handle the re-directs in the site contact create and update methods (below)

site_contacts_controller.rb

  def create
    @site = Site.friendly.find(params[:site_id])
    @site_contact = SiteContact.new(site_contact_params)
    @site_contact.site = @site

    respond_to do |format|
      if @site_contact.save
        format.html { redirect_to @site_contact, notice: 'Site contact was successfully created.' }
        format.json { render :show, status: :created, location: @site_contact }
      else
        format.html { render :new }
        format.json { render json: @site_contact.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    @site = Site.friendly.find(params[:site_id])
    respond_to do |format|
      if @site.update(site_contact_params)
        format.html { redirect_to @site_contact, notice: 'Site contact was successfully updated.' }
        format.json { render :show, status: :ok, location: @site_contact }
      else
        format.html { render :edit }
        format.json { render json: @site_contact.errors, status: :unprocessable_entity }
      end
    end
  end

I originally changed @site_contact to @site in both create and update methods and it was throwing the error, I changed them back to original for now as it clearly wasn't working.

Im lost here and am having a hell of a time trying to find a solution. Any help would be greatly appreciated!

Thanks in advance, please let me know if you require any further details.

EDIT #1: Adds rake routes output for sites

     site_site_contacts POST   /sites/:site_id/site_contacts(.:format)                  sites/site_contacts#create
  new_site_site_contact GET    /sites/:site_id/site_contacts/new(.:format)              sites/site_contacts#new
 edit_site_site_contact GET    /sites/:site_id/site_contacts/:id/edit(.:format)         sites/site_contacts#edit
      site_site_contact GET    /sites/:site_id/site_contacts/:id(.:format)              sites/site_contacts#show
                        PATCH  /sites/:site_id/site_contacts/:id(.:format)              sites/site_contacts#update
                        PUT    /sites/:site_id/site_contacts/:id(.:format)              sites/site_contacts#update
                        DELETE /sites/:site_id/site_contacts/:id(.:format)              sites/site_contacts#destroy

Here are the client/sites rake routes output

       client_sites POST   /clients/:client_id/sites(.:format)                      clients/sites#create
    new_client_site GET    /clients/:client_id/sites/new(.:format)                  clients/sites#new
   edit_client_site GET    /clients/:client_id/sites/:id/edit(.:format)             clients/sites#edit
        client_site GET    /clients/:client_id/sites/:id(.:format)                  clients/sites#show
                    PATCH  /clients/:client_id/sites/:id(.:format)                  clients/sites#update
                    PUT    /clients/:client_id/sites/:id(.:format)                  clients/sites#update
                    DELETE /clients/:client_id/sites/:id(.:format)                  clients/sites#destroy

So I do see an issue here in the outputs, it should be redirecting to client/:client_id/sites/:site_id/site_contacts/:id as there is no sites controller, or no sites/show it is nested to clients.

EDIT #2 New ERROR

No route matches {:action=>"show", :controller=>"sites/site_contacts", :site_id=>#<SiteContact id: 3, site_id: 1, name: "Shawn Wilson", tel_number: "403-615-5915", tel_ext: "", email: "swilson@taurenltd.com", contact_type: "Management", after_hours: true, notes: "Primary", created_at: "2016-09-23 06:47:42", updated_at: "2016-09-23 06:47:42", slug: "shawn-wilson-b7a18908-34ae-4385-bcf7-17c97547dabb">} missing required keys: [:id]
Ajay Barot
  • 1,681
  • 1
  • 21
  • 37
Shawn Wilson
  • 1,311
  • 14
  • 40
  • Could you include result of running `rake routes`? – xuanduc987 Sep 23 '16 at 06:35
  • @xuanduc987 My apologies, I will add that right now! – Shawn Wilson Sep 23 '16 at 06:36
  • @xuanduc987 Updated! – Shawn Wilson Sep 23 '16 at 06:40
  • Maybe change `format.html { redirect_to @site_contact, notice: 'Site contact was successfully created.' }` and `format.json { render :show, status: :created, location: @site_contact }` to `format.html { redirect_to site_site_contact_path(@site_contact), notice: 'Site contact was successfully created.' }` and `format.json { render :show, status: :created, location: site_site_contact_path(@site_contact), }` could work – xuanduc987 Sep 23 '16 at 06:44
  • @xuanduc987 See above for the error that throws – Shawn Wilson Sep 23 '16 at 06:48
  • 1
    My memory is hazy, so maybe using `site_site_contract_path(@site, @site_contact)` should work. – xuanduc987 Sep 23 '16 at 07:18
  • @xuanduc987 if u wanna put that in an asnwer ill give it best.. It submits the form, just need to figure out page links but that gets me back on the road! thanks – Shawn Wilson Sep 23 '16 at 07:22

0 Answers0