1

I have a TodoList model and a TodoItem model. I can create the nested form and it renders in the todolist#new. It will only create a todolist title. It will accept the todo list items, but on the index page the items are not showing up. I have done a ton of research including the docs and these sources; see bottom of the post.

The form will render with no errors. But when I hit save the todo list title will save, but the items I enter will not save.

Todo List Title --This will save

Items ---This will not save

Controller

class TodoListsController < ApplicationController
  before_filter :authenticate_user!
  before_filter except: [:index]
  before_action :set_todo_list, only: [:show, :edit, :update, :destroy]

  # GET /todo_lists
  # GET /todo_lists.json
  def index
    #@todo_lists = TodoList.all
    @todo_lists = current_user.todo_lists
    @todo_items = current_user.todo_items

  end

  # GET /todo_lists/1
  # GET /todo_lists/1.json
  def show
  end

  # GET /todo_lists/new
  def new
    @todo_list = current_user.todo_lists.new
    @todo_list.todo_items.build
    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @todo_list }
    end
  end

  # GET /todo_lists/1/edit
  def edit
    @todo_list = TodoList.find(params[:id])
  end

  # POST /todo_lists
  # POST /todo_lists.json
  def create
    #@todo_list = TodoList.new(todo_list_params)
    @todo_list = current_user.todo_lists.new(todo_list_params)
    respond_to do |format|
      if @todo_list.save
        format.html { redirect_to @todo_list, notice: 'Todo list 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

  # PATCH/PUT /todo_lists/1
  # PATCH/PUT /todo_lists/1.json
  def update
    @todo_list = TodoList.find(params[:id])
    respond_to do |format|
      if @todo_list.update(todo_list_params)
        format.html { redirect_to @todo_list, notice: 'Todo list was successfully updated.' }
        format.json { render :show, status: :ok, location: @todo_list }
      else
        format.html { render :edit }
        format.json { render json: @todo_list.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /todo_lists/1
  # DELETE /todo_lists/1.json
  def destroy
    #@todo_list.TodoList.find(params[:id])
    @todo_list.destroy
    respond_to do |format|
      format.html { redirect_to todo_lists_url, notice: 'Todo list was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    def owns_todolist
      if current_user != TodoList.find(params[:id]).user
        redirect_to todo_lists_path, error: "You can't do that!"
      end
    end
    # Use callbacks to share common setup or constraints between actions.
    def set_todo_list
      @todo_list = TodoList.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def todo_list_params
      params.require(:todo_list).permit(:title,:todo_list => [:description])
    end
end

Models

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

    end


    class TodoItem < ActiveRecord::Base
        belongs_to :todo_list

    end

Form

<%= form_for(@todo_list) do |f| %>
  <% if @todo_list.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@todo_list.errors.count, "error") %> prohibited this todo_list from being saved:</h2>

      <ul>
      <% @todo_list.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
  <div class="field">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>

  <%= f.fields_for :todo_items do |b| %> 
   <--- I also tried passing this in fields for @todo_list.todo_items.build -- >
    <%= b.label :description, "Items" %><br />
    <%= b.text_field :description %>
  <% end %>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Previous research

Nested Form with Nested Resource Rails 4

Rails 4 Nested Form.

How to Create Nested Forms in Rails 4

http://www.createdbypete.com/articles/working-with-nested-forms-and-a-many-to-many-association-in-rails-4/

http://railscasts.com/episodes/196-nested-model-form-revised?view=comments

Rails 4 Nested form fields not saving in database

And the list goes on.

1 Answer 1

1

The problem is with your todo_list_params method.It should be like this

def todo_list_params
 params.require(:todo_list).permit(:title,todo_items_attributes: [:description])
end
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Every resource has a different answer and I tried all of them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.