Using Strong Parameters With Fields for & Nested Forms in Rails 4

With strong_parameters becoming the standard way of handling security in Rails 4, I played around with it. It works great except the documentation isn’t clear on how to handle nested forms inside Rails, specifically with the accepts_nested_attributes_for in the model and fields_for in views.
So far so good, let take a look a short example below.

account.rb
1
2
3
4
class Account < ActiveRecord::Base
  has_many :people
  accepts_nested_attributes_for :people
end
person.rb
1
2
3
class Person < ActiveRecord::Base
  belongs_to :account
end
accounts_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class AccountsController < ApplicationController
  def new
    @account = Account.new
    @account.people.build
  end

  def create
    @account = Account.new(new_account_params)
    if @account.save
      respond_to do |format|
        format.html {redirect_to root_path, notice: "Account created successfully."}
      end
    end
  end

  private
  def new_account_params
    params.require(:account).permit(:id, :name, people_attributes: [:id, :email, :password, :password_confirmation])
  end
end