2

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

1 Answer 1

4

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.