How to support multiparameter attributes in a rails form object?

612 views Asked by At

I have a form object using the pattern defined here: https://forum.upcase.com/t/form-objects/2267

class RegistrationForm
  include ActiveModel::Model

  attr_accessor :first_name, :last_name, :date_of_birth
  ...
end

Unlike that example, one of the underlying models in my form has a date field. That date field is being presented as a multiparameter attribute by simple form, and is submitted through to the controller as such:

"user"=>
  {"first_name"=>"Hanna",
   "last_name"=>"Macias",
   "date_of_birth(1i)"=>"2016",
   "date_of_birth(2i)"=>"2",
   "date_of_birth(3i)"=>"26",
...

However I'm getting a unknown attribute 'date_of_birth(1i)' for RegistrationForm. error from RegistrationForm.new(registration_params) in the #create action on the controller:

def create
  @registration = RegistrationForm.new(registration_params)
  if @registration.save
    redirect_to root_path
  else
    render 'new'
  end
end

private

def registration_params
  params.require(:registration).permit!
end

I've tried defining additional attr_accessors for the :date_of_birth_1i, :date_of_birth_2i, :date_of_birth_3i, etc., but that hasn't resolved the issue.

What do I need to do to handle the multiparameter attribute correctly?

1

There are 1 answers

0
tommarshall On

I need to include ActiveRecord::AttributeAssignment as well as ActiveModel::Model, and that solved the issue.