Reputation: 919
In my app, the user fills out a form and creates the parent class - Cost
Later, I want the user to be able to go back in to add the associative class - Cost Dependencies.
So the user gets navigated to /costs/3/cost_dependencies/new
When I try to reference @cost
in my view, I get a nil class error, So I added @cost=Cost.find(param[:id])
to access it in my controller.
Now I'm getting a Couldn't find Cost with id=
error
It's just blank.
Upvotes: 0
Views: 28
Reputation: 24337
Assuming you have set up nested routes like:
resources :costs do
resources :cost_dependencies
end
Then the cost id will be available as params[:cost_id]
, which you can confirm by running rake routes
. You should see something like the following:
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
So, in your controller use the following:
@cost = Cost.find(param[:cost_id])
Upvotes: 1