ActiveRecord::RecordNotFound in ContactsController#create Couldn't find Company without an ID error

StackOverflow https://stackoverflow.com/questions/17661975

  •  03-06-2022
  •  | 
  •  

質問

When i try to submit at localhost:3000/companies/1/contacts/new i get the error Couldn't find Company without an ID https://gist.github.com/overhang/f8c20d2d2c851cdee7b1 any clue? I reckon it might be a problem with routes.rb

役に立ちましたか?

解決

Remove the following lines from routes.rb

# config/routes.rb
# get "companies/index"

# get "companies/new"

# get "companies/show"

# get "companies/create"

# get "companies/edit"

Notice that RESTful controller actions like edit and show require a specific Company passed in order for the correct company to be looked up. These get routes don't allow for that. Instead, you should be utilizing the resource routes you've already created:

# config/routes.rb
resources :companies

The non-resourceful routes are impeding the execution of the resourceful ones. Removing them should fix your problem.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top