-1

I was trying to work with simple nested models & forms but have failed eventually. I don't understand what wrong am I doing? Trying to implement simple nested model. A parent having many child. Can anyone please help me out. Thanks.

Here are the models:

parent.rb

class Parent < ActiveRecord::Base
has_many :childs
accepts_nested_attributes_for :childs
end

child.rb

class Child < ActiveRecord::Base
belongs_to :parent
end

parents_controller.rb

class ParentsController < ApplicationController
  before_action :set_parent, only: [:show, :edit, :update, :destroy]
  # GET /parents
  # GET /parents.json
  def index
    @parents = Parent.all
  end

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

  # GET /parents/new
  def new
    @parent = Parent.new
    @parent.childs.new
  end

  # GET /parents/1/edit
  def edit
  end

  # POST /parents
  # POST /parents.json
  def create
    @parent = Parent.create(parent_params)
    respond_to do |format|
      if @parent.save
        format.html { redirect_to @parent, notice: 'Parent was successfully created.' }
        format.json { render :show, status: :created, location: @parent }
      else
        format.html { render :new }
        format.json { render json: @parent.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /parents/1
  # PATCH/PUT /parents/1.json
  def update
    respond_to do |format|
      if @parent.update(parent_params)
        format.html { redirect_to @parent, notice: 'Parent was successfully updated.' }
        format.json { render :show, status: :ok, location: @parent }
      else
        format.html { render :edit }
        format.json { render json: @parent.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /parents/1
  # DELETE /parents/1.json
  def destroy
    @parent.destroy
    respond_to do |format|
      format.html { redirect_to parents_url, notice: 'Parent was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_parent
      @parent = Parent.find(params[:id])
    end
    def parent_params
      params.require(:parent).permit(:name, childs_attributes: [:name])
    end
end

parent form.html.erb

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

      <ul>
      <% @parent.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label 'Parent name' %><br>
    <%= f.text_field :name %>
  </div>
  <%= f.fields_for :child do |c| %>
  <div class="field">
    <%= c.label 'Child Name' %><br>
    <%= c.text_field :name %>
  </div>
  <% end %>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

The parent is getting created but the child is not. The child table has parent_id for associating both models.

Thanks in advance

1 Answers1

1

I believe the plural of child is not childs, but children, and Rails knows this. You need to change your has_many association accordingly.

EDIT: As @pavan pointed out, change all the occurences in your code, not only the association.

born4new
  • 1,677
  • 12
  • 12
  • Not just only in association but other places in the code too should be changed to children. – Pavan May 19 '16 at 15:34
  • Thanks a lot @Pavan. Made the changes as you both prompted and it worked. That was really childish of me to guess that rails won't understand the plural of child as children. – Ashish Wadekar May 20 '16 at 01:23