Ruby on Rail Nested Attributes do not save to database

122 views Asked by At

I am trying to create a form that updates 2 tables - commission_type and commission_tier.

I created the models, controller and form but when I submit it, my commission_tier table does not update. Only my commission_type table updates.

Can someone take a look at my code and tell me what I am doing wrong? I have combed through my code trying to find the mistake, and I cannot find it.

My models

class CommissionType < ApplicationRecord
  has_many :commission_tiers
  accepts_nested_attributes_for :commission_tiers
end
class CommissionTier < ApplicationRecord
  belongs_to :commission_types, optional: true
end

My controller

class Admin::CommissionTypesController < Admin::BaseController


  def index
    @commission_types = CommissionType.all
  end

  def new
    @commission_type = CommissionType.new
    @commission_type.commission_tiers.build
  end

def create
    @commission_type = CommissionType.new(commission_type_params)
    if @commission_type.save
      redirect_to admin_commission_types_index_path
    else
      render "new"
    end
  private

  def commission_type_params
    params.require(:commission_type).permit(:name, :active, :allow_mass_update, :plan,
                                               commission_tiers_attributes: [:id, :increment_value, :rate, :commission_type_id])
  end
end

My form

<%= simple_form_for @commission_type, url: admin_commission_types_index_path, wrapper: :bootstrap2, :html => { :class => 'form-horizontal' } do |f| %>
      <fieldset>
        <legend>Properties</legend>
        <%= f.input :name, label: 'Commission Name' %>

        <%= f.input :active, as: :boolean, label: 'Active?', label_html: { class: 'padding-top' } %>
        <%= f.input :allow_mass_update, as: :boolean,  label: 'Allow mass update?', label_html: { class: 'padding-top' } %>
        <%= f.input :plan, input_html: {id: 'dropdown'},
                    label: 'Commission Type', 
                    collection: [ ['Select One..', 'select'], ['Flat', 'flat'], ['Flat +', 'flat_plus' ], ['Promotional', 'promotional'], ['Straight', 'straight'], ['Waterfall', 'waterfall'],  ['Sliding Scale', 'sliding_scale'] ],
                    selected: 'select'
                    %>
      </fieldset>
     
        
      <fieldset id="flat">
        <legend>Flat Commission</legend>
        <%= f.simple_fields_for :commission_tiers do |builder| %>
          <%= builder.input :rate %>
          <%= builder.input :increment_value %>
        <% end %>
      </fieldset>

My form is displaying and working html form

UPDATE

Some additional details

CommissionType column values = [:name, :active, :allow_mass_update, :plan]

CommissionTier column values = [:id, :increment_value, :rate, :commission_type_id]

Also, when I submit my form, here is an example of what my params are

<ActionController::Parameters {"name"=>"asdf", "active"=>"1", "allow_mass_update"=>"1", "plan"=>"flat", "commission_tiers_attributes"=><ActionController::Parameters {"0"=><ActionController::Parameters {"rate"=>"45"} permitted: true>} permitted: true>} permitted: true>
0

There are 0 answers