Facing issue in edit-update action in nested form in rails4?

42 views Asked by At

Hi I am using gem "nested_form" and included has_many association in my app sample code is :

class Question < ActiveRecord::Base
  has_many :choices
  accepts_nested_attributes_for :choices
end

and in my controller have included this :

class QuestionsController < ApplicationController
  before_action :set_questions, only:  [:edit, :update]
  def edit

  end

  def update    
    if @question.update_attributes(question_params)
      redirect_to questions_path
    else
      render :action => :edit
    end
  end

  private

  def set_questions
    @question = Question.where(:id => params[:id]).first
  end

  def question_params
    params.require(:question).permit(:content,
                                     choices_attributes: [:option, :is_correct,
                                                         :question_id])
  end
end

and in edit.html.erb

<%= nested_form_for @question do |f|%>
  <%= f.label :content %>
  <%= f.text_field :content %>
  <%= f.fields_for :choices do |c|  %>
    <%= c.label  :option %>
    <%= c.text_field :option %>
    <%= c.check_box :is_correct%>
    <%= c.label :is_correct %>
  <% end %>
  <%= f.link_to_add "Add Choices", :choices%>
  </br>
  <%= f.submit %>
<% end %>

so in edit it adds choices even they are present and I have not even edit/add any of choices.
If I already have 3 choices with respect to question_id=1 so at the time of edit I have not edited any of choices nor I have added any new for that question_id but then too at the time of submit it creates 3 more choices. It gives this params on submit

Parameters: {"utf8"=>"✓", "authenticity_token"=>"jTLaIz0BdKbSZgnMl4T2GhZyYbKvo0JG2VD8e1zbvQGp6ILyKqLOZy19QvZrXhVGr5OClcwibWL0HJwIAGJ/rQ==", "question"=>{"content"=>"Business logic is defined in ?", "choices_attributes"=>{"0"=>{"option"=>"Model", "is_correct"=>"1", "id"=>"36"}, "1"=>{"option"=>"view", "is_correct"=>"0", "id"=>"37"}, "2"=>{"option"=>"controller", "is_correct"=>"0", "id"=>"38"}, "3"=>{"option"=>"helpers", "is_correct"=>"0", "id"=>"39"}}}, "commit"=>"Update Question", "id"=>"10"}

Please guide me how to solve this. Thanks in advance.

3

There are 3 answers

1
Pavan On BEST ANSWER

The problem is in your question_params. You have to add :id for edit/update to work correctly else it will create new records on every successful submit.

def question_params
    params.require(:question).permit(:id, :content, choices_attributes: [:id, :option, :is_correct, :question_id])
end
0
user4965201 On

You rails console must be giving unpermitted parameter as :id because you haven't passed id to the update_attributes thats why it is creating new object , all you need to do is

def question_params
    params.require(:question).permit(:id, :content, choices_attributes: [:id, :option, :is_correct, :question_id])
end
0
Yogesh Khater On

It might be happening because you haven't permitted id of choice in choices_attributes.

nested_form treats every choice attributes as creating new record on submit if it don't contain id.