Rails:在创建父级后创建 belongs_to 关联 class
Rails: creating belongs_to associative class after parent has been created
在我的应用中,用户填写表单并创建父级 class - 成本
稍后,我希望用户能够返回以添加关联 class - 成本依赖性。
所以用户被导航到 /costs/3/cost_dependencies/new
当我尝试在我的视图中引用 @cost
时,我得到一个 nil class 错误,所以我添加了 @cost=Cost.find(param[:id])
以在我的控制器中访问它。
现在我遇到 Couldn't find Cost with id=
错误
只是一片空白。
假设您设置了嵌套路由,例如:
resources :costs do
resources :cost_dependencies
end
然后成本 ID 将可用为 params[:cost_id]
,您可以通过 运行 rake routes
确认。您应该会看到如下内容:
cost_cost_dependencies GET /costs/:cost_id/cost_dependencies(.:format) cost_dependencies#index
POST /costs/:cost_id/cost_dependencies(.:format) cost_dependencies#create
new_cost_cost_dependency GET /costs/:cost_id/cost_dependencies/new(.:format) cost_dependencies#new
edit_cost_cost_dependency GET /costs/:cost_id/cost_dependencies/:id/edit(.:format) cost_dependencies#edit
cost_cost_dependency GET /costs/:cost_id/cost_dependencies/:id(.:format) cost_dependencies#show
PATCH /costs/:cost_id/cost_dependencies/:id(.:format) cost_dependencies#update
PUT /costs/:cost_id/cost_dependencies/:id(.:format) cost_dependencies#update
DELETE /costs/:cost_id/cost_dependencies/:id(.:format) cost_dependencies#destroy
costs GET /costs(.:format) costs#index
POST /costs(.:format) costs#create
new_cost GET /costs/new(.:format) costs#new
edit_cost GET /costs/:id/edit(.:format) costs#edit
cost GET /costs/:id(.:format) costs#show
PATCH /costs/:id(.:format) costs#update
PUT /costs/:id(.:format) costs#update
DELETE /costs/:id(.:format) costs#destroy
因此,在您的控制器中使用以下内容:
@cost = Cost.find(param[:cost_id])
在我的应用中,用户填写表单并创建父级 class - 成本
稍后,我希望用户能够返回以添加关联 class - 成本依赖性。
所以用户被导航到 /costs/3/cost_dependencies/new
当我尝试在我的视图中引用 @cost
时,我得到一个 nil class 错误,所以我添加了 @cost=Cost.find(param[:id])
以在我的控制器中访问它。
现在我遇到 Couldn't find Cost with id=
错误
只是一片空白。
假设您设置了嵌套路由,例如:
resources :costs do
resources :cost_dependencies
end
然后成本 ID 将可用为 params[:cost_id]
,您可以通过 运行 rake routes
确认。您应该会看到如下内容:
cost_cost_dependencies GET /costs/:cost_id/cost_dependencies(.:format) cost_dependencies#index
POST /costs/:cost_id/cost_dependencies(.:format) cost_dependencies#create
new_cost_cost_dependency GET /costs/:cost_id/cost_dependencies/new(.:format) cost_dependencies#new
edit_cost_cost_dependency GET /costs/:cost_id/cost_dependencies/:id/edit(.:format) cost_dependencies#edit
cost_cost_dependency GET /costs/:cost_id/cost_dependencies/:id(.:format) cost_dependencies#show
PATCH /costs/:cost_id/cost_dependencies/:id(.:format) cost_dependencies#update
PUT /costs/:cost_id/cost_dependencies/:id(.:format) cost_dependencies#update
DELETE /costs/:cost_id/cost_dependencies/:id(.:format) cost_dependencies#destroy
costs GET /costs(.:format) costs#index
POST /costs(.:format) costs#create
new_cost GET /costs/new(.:format) costs#new
edit_cost GET /costs/:id/edit(.:format) costs#edit
cost GET /costs/:id(.:format) costs#show
PATCH /costs/:id(.:format) costs#update
PUT /costs/:id(.:format) costs#update
DELETE /costs/:id(.:format) costs#destroy
因此,在您的控制器中使用以下内容:
@cost = Cost.find(param[:cost_id])