ruby-on-railsnested-resources

rails: nested resources saving empty records


I have created a simple form of nested resources. I can't get the child form todo_item to save correctly. When I click the "create todo item" button, an empty record will be created and saved no matter what I put in the title field.

The code is really simple and the parent record is saving fine. the child records are displaying so I can't figure out what could be wrong. Please help.

My models:

class TodoItem < ActiveRecord::Base
  belongs_to :todo_list
end

class TodoList < ActiveRecord::Base
  has_many :todo_items, dependent: :destroy
end

My controllers:

class TodoItemsController < ApplicationController
  before_action :set_todo_list
  before_action :set_todo_item, only: [:show, :edit, :update, :destroy]

  def new
    @todo_item = @todo_list.todo_items.new
  end

  def create
    @todo_item = @todo_list.todo_items.new

    respond_to do |format|
      if @todo_list.save
        format.html { redirect_to @todo_list, notice: 'Todo item was successfully created.' }
        format.json { render :show, status: :created, location: @todo_list }
      else
        format.html { render :new }
        format.json { render json: @todo_list.errors, status: :unprocessable_entity }
      end
    end
  end

  private

    def set_todo_item
      @todo_item = @todo_list.todo_items.find(params[:id])
    end

    def set_todo_list
      @todo_list = TodoList.find(params[:todo_list_id])
    end

    def todo_item_params
      params.require(:todo_item).permit(:title, :due_date, :description, :text, :completed)
    end

end

View.html.erb:

<h1>New Todo Item</h1>

<%= render 'form' %>

<%= link_to 'Back', @todo_list %>

_form.html.erb:

<%= form_for([@todo_list, @todo_item]) do |f| %>
 ...

  <div class="field">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

enter image description here


Solution

  • You need to modify the create method:

    def create
    @todo_item = @todo_list.todo_items.create(todo_item_params)
    
    respond_to do |format|
      if @todo_list.save
        format.html { redirect_to @todo_list, notice: 'Todo item was successfully created.' }
        format.json { render :show, status: :created, location: @todo_list }
      else
        format.html { render :new }
        format.json { render json: @todo_list.errors, status: :unprocessable_entity }
      end
    end
    

    end