mardi 6 octobre 2020

How to retain PORO Form object field inputs?

I've created a R-o-R Form object which takes in attributes and then saves those attributes to some nested objects, however, upon failing the validations, the input values disappear. Is there anyway to retain them?

class FormObject
 include ActiveModel::Model
 attr_accessor(:name, :date)
 
 def initialize(params = {})
  @params = params
 end

 def save
  return if invalid?
  no = NestedObject.new(nested_object_params)
  no.save
 end

 def nested_object_params
  @params.permit(:name, :date)
 end
end

and this is the controller

class Controller
 def new
  @form = FormObject.new
 end

 def create
  @form = FormObject.new(form_object_params)
  if @form.save
    redirect_to ...
  else
    render :new
  end
 end

 def form_object_params
  params.require(:form_object).permit(:name, :date)
 end
end

Aucun commentaire:

Enregistrer un commentaire