Search code examples
ruby-on-railsruby-on-rails-6nested-attributes

Rails 6: Nested Form not saving


I have been trying to get a simple test example of a nested form in Rails 6 running but so far I have not been able to create an example that works. As far as I know I created a correct association in my models, added a nested form, permitted parameters to accept nested attributes. Whenever I submit the form I get Completed 422 Unprocessable Entity this is where I hit a bit of a wall and don't know what to do to continue.

Poll Model

class Poll < ApplicationRecord
  has_many :questions
  accepts_nested_attributes_for :questions, allow_destroy: true, reject_if: :all_blank
  validates_presence_of :title
end

Question Model

class Question < ApplicationRecord
  belongs_to :poll
  validates_presence_of :content, :poll_id
end

Poll Controller

class PollsController < ApplicationController
before_action :set_poll, only: %i[ show edit update destroy ]

# GET /polls or /polls.json
  def index
  @polls = Poll.all
end

# GET /polls/1 or /polls/1.json
def show
end

# GET /polls/new
def new
  @poll = Poll.new
  2.times { @poll.questions.build}
end

# GET /polls/1/edit
  def edit
end

# POST /polls or /polls.json
def create
  @poll = Poll.new(poll_params)
  respond_to do |format|
    if @poll.save
     format.html { redirect_to @poll, notice: "Poll was successfully created." }
     format.json { render :show, status: :created, location: @poll }
    else
     # raise poll_params.inspect  
     format.html { render :new, status: :unprocessable_entity }
     format.json { render json: @poll.errors, status: :unprocessable_entity }
   end
 end
end

# PATCH/PUT /polls/1 or /polls/1.json
def update
  respond_to do |format|
    if @poll.update(poll_params)
      format.html { redirect_to @poll, notice: "Poll was successfully updated." }
      format.json { render :show, status: :ok, location: @poll }
    else
      format.html { render :edit, status: :unprocessable_entity }
      format.json { render json: @poll.errors, status: :unprocessable_entity }
    end
  end
end

# DELETE /polls/1 or /polls/1.json
def destroy
 @poll.destroy
 respond_to do |format|
   format.html { redirect_to polls_url, notice: "Poll was successfully destroyed." }
   format.json { head :no_content }
 end
end

private
  # Use callbacks to share common setup or constraints between actions.
 def set_poll
   @poll = Poll.find(params[:id])
 end

 # Only allow a list of trusted parameters through.
 def poll_params
   params.require(:poll).permit(:title, :questions_attributes => [:id, :content, :_destroy] )
 end
end

_form.html.erb

<%= form_for(@poll) do |f| %>
...

<fieldset>
<div>
    <%= f.label :title %>
    <%= f.text_field :title %>
</div>

    <legend>Questions:</legend>
    <%= f.fields_for :questions do |questions_form| %>
        <%= render "question_fields", f: questions_form %>
    <% end %>

    <%= link_to_add_fields "Add Questions", f, :questions %>
    <input name="authenticity_token" 
           type="hidden" 
           value="<%= form_authenticity_token %>"/>
</fieldset>

<%= f.submit %>
<% end %>

_question_fields.html.erb

<div class="nested-fields">
<%= f.hidden_field :_destroy %>
<div>
    <%= f.label :content %>
    <%= f.text_field :content %>
</div>
<div>
    <%= link_to "Remove", '#', class: "remove_fields" %>
</div>
</div>

When I submit the form I get

Started POST "/polls" for ::1 at 2021-04-06 16:03:15 +0900
Processing by PollsController#create as HTML
Parameters: {"authenticity_token"=>"[FILTERED]", "poll"=>{"title"=>"Test Poll", 
"questions_attributes"=>{"0"=>{"_destroy"=>"false", "content"=>"Question 1"}, "1"=> 
{"_destroy"=>"false", "content"=>"Question 2"}}}, "commit"=>"Create Poll"}
Rendering layout layouts/application.html.erb
Rendering polls/new.html.erb within layouts/application
Rendered polls/_question_fields.html.erb (Duration: 0.5ms | Allocations: 296)
Rendered polls/_question_fields.html.erb (Duration: 0.4ms | Allocations: 294)
Rendered polls/_question_fields.html.erb (Duration: 0.7ms | Allocations: 285)
Rendered polls/_form.html.erb (Duration: 4.1ms | Allocations: 1695)
Rendered polls/new.html.erb within layouts/application (Duration: 4.5ms | Allocations: 1804)
[Webpacker] Everything's up-to-date. Nothing to do
Rendered layout layouts/application.html.erb (Duration: 92.4ms | Allocations: 8240)
Completed 422 Unprocessable Entity in 115ms (Views: 93.4ms | ActiveRecord: 0.0ms | Allocations: 10383)

Solution

  • The Poll isn't created at the moment the validations in the Question model are run and so the question's poll_id is nil causing the presence validation to fail. You can remove the validation since the belongs_to :poll association validates that the poll exists (belongs_to association is required by default from Rails 5).

    class Question < ApplicationRecord
      belongs_to :poll
      validates_presence_of :content
    end