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?

tommarshall
  • 2,038
  • 5
  • 23
  • 36
  • maybe this can help you, http://guides.rubyonrails.org/form_helpers.html#nested-forms and also this api page http://edgeapi.rubyonrails.org/classes/ActionController/Parameters.html I believe your params should be something like `params.require(:user).permit(:first_name)`. The best is set a `binding.pry` on this `post` request, then in the console check the `params hash` and how to set the method `registration_params` to include also `first_name`. Maybe you need `params.require("user").permit("first_name")`, test this in the console and see if it retrieves your input – Fabrizio Bertoglio Oct 20 '17 at 15:20

1 Answers1

4

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

tommarshall
  • 2,038
  • 5
  • 23
  • 36