ga-wdi-exercises / project2

[project]
1 stars 30 forks source link

problems with Create #870

Closed edawson1980 closed 7 years ago

edawson1980 commented 7 years ago

hey guys -

I have a question regarding my Create method. Everything was working properly at the MVP level for my app, but I've since attempted to nest my resources (Projects are nested inside of Docs). When I create a new project, I receive an error upon hitting the submit button: Doc not found

I've cross-checked everything I can think of, and compared my code to tunr examples, and I still can't figure out what I'm missing. I don't expect a response until tomorrow, and I'll update this issue if I figure it out between now and then.

Thanks, Erin

screen shot 2017-04-30 at 2 49 57 pm

https://github.com/edawson1980/story-doc/blob/master/app/controllers/projects_controller.rb#L12-L16

https://github.com/edawson1980/story-doc/blob/master/app/models/project.rb#L1-L3

https://github.com/edawson1980/story-doc/blob/master/app/views/projects/new.html.erb#L1-L8

https://github.com/edawson1980/story-doc/blob/master/db/schema.rb

https://github.com/edawson1980/story-doc/blob/master/db/seeds.rb

https://github.com/edawson1980/story-doc/blob/master/config/routes.rb

juancgarcia commented 7 years ago

I think the issue here is not to do with your routes, but with the relationships of your models.

The error is telling us Validation failed: Doc must exist. Since Project belongs to Doc a new project record in the database must have a Doc assigned or ActiveRecord will complain like this.

If we look at your create controller action. app/controllers/projects_controller.rb#L12-L16 we'll see that you're finding a Doc and assigning it to @doc on line 13, but on line 14 when creating the new Project instance, @project = Project.create!(project_params) you aren't providing @doc.

In much the same way that we added a user to Post.create hashes in the Devise lesson, we can use .merge here to add the required doc for creating your Project instance.

@project = Project.create!(project_params.merge(doc: @doc))
edawson1980 commented 7 years ago

fantastic, thank you!