在我的应用中,用户填写表单并创建父类 - 成本
稍后,我希望用户能够返回添加关联类 - Cost Dependencies。
因此用户可以导航到/ costs / 3 / cost_dependencies / new
当我尝试在我的视图中引用@cost
时,我收到了一个nil类错误,所以我添加了@cost=Cost.find(param[:id])
来在我的控制器中访问它。
现在我收到Couldn't find Cost with id=
错误
它只是空白。
答案 0 :(得分:1)
假设您已设置嵌套路线,例如:
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])