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