anastasiaalt / quiz

Anastasia's Ruby on Rails Project
1 stars 0 forks source link

Editing Quiz Duplicates Questions vs. ONLY Changing Existing Questions #5

Closed anastasiaalt closed 8 years ago

anastasiaalt commented 8 years ago

When I go to edit an existing quiz,

http://localhost:3000/quizzes/1/edit

for example, the quiz with all the questions, options, etc appears. When I change any or multiple fields and then submit it, the changes are accepted but a new set of all the questions and their now changed details are added to the quiz. For example, if I had three questions, change one question, and then submit this change, the result is the quiz will now have six questions: three of the original questions unchanged, and then three new questions including the changed question.

This is strange to me as I should just be using the edit and update methods. Really not sure where this could be coming from as procedurally it follows what I have done each time for edits. I checked in the rails console and there are still only two Quizes (i.e. a new, changed quiz is not being made each time) so I don't see at what pt the creation of effectively new questions with options is happening in the process.

Here are the links to the relevant files:

https://github.com/anastasiaalt/quiz/blob/master/app/controllers/quizzes_controller.rb https://github.com/anastasiaalt/quiz/blob/master/app/views/quizzes/edit.html.erb https://github.com/anastasiaalt/quiz/blob/master/app/views/quizzes/show.html.erb

anastasiaalt commented 8 years ago

Hi, this issue is also outstanding. Both issues would be helpful to get some pointers. Thanks!

Avizacherman commented 8 years ago

Anastasia, I'll be honest, I'm not super familiar with nested attributes but I believe that is what's causing the issue. You don't pass an id param in for the questions and I believe it cannot update the proper question because it can't look them up. As a result, Active Record is trying to be helpful by creating new ones. It's still updating the overall quiz and it's updated it's nested questions it just had to make new ones.

short-matthew-f commented 8 years ago

Anastasia — I’ll be in today to help you with this, I think at 9:30.  I apologize, kid + wife means I’m off the computer overnight.  

--  Matt Short Sent with Airmail

On February 12, 2016 at 7:19:39 AM, Avi Zacherman (notifications@github.com) wrote:

Anastasia, I'll be honest, I'm not super familiar with nested attributes but I believe that is what's causing the issue. You don't pass an id param in for the questions and I believe it cannot update the proper question because it can't look them up. As a result, Active Record is trying to be helpful by creating new ones. It's still updating the overall quiz and it's updated it's nested questions it just had to make new ones.

— Reply to this email directly or view it on GitHub.

Avizacherman commented 8 years ago

Just found this very short comment on stack overflow to back up what I said:

"DON'T FORGET THE ID!!!! pets_attributes: [:id, :name, :category] Otherwise, when you edit, each pet will get created again. – Arcolye Jun 3 '13 at 3:12"