Class Person Individuals Individuals. Rails 4

0

I'm developing a APP in Rails , and the following question came up for the Person class.

The structure of these classes looks like this:

Pessoa { tipo, status }
PessoaJuridica { razãoSocial, nomeFantasia, CNPJ, IE, IM }
PessoaFisica { nome, apelido, CPF, RG }

In my view of Person in _form.html.erb I have radio_button to select the type of person to be registered ( Fisica ou Juridica ), but I am in doubt how to create form fields. In this case, the relationship is has_one from Person to PessoaFisica/Juridica , how would I create this _form to have the fields both in the PessoaFisica and PessoaJuridica table and change them depending on radio_button selection? / p>

I thought about creating two% of one% for physical person, and another for legal and putting them in _forms with view ; and show them with style display: none when they are selected in jQuery , is this valid? And how would radio_button of Person save the registration, how will I identify at Create time, whether it is a legal person or a person? and in which Model to save?

Thank you in advance, Hugs.

EDIT

I was able to create controller with all fields showing only the fields that the user selects with _form . The problem is now when saving the register, as in practice I have the fields of two Model in radio_button just 'hiding' (. Hide ()) the fields that are not of the _form selection, when I confirm the registration , you are saving a radio_button and a pessoa_juridica , and the one that was not selected in pessoa_fisica is left blank (""). How do I resolve this?

    
asked by anonymous 24.02.2016 / 16:47

1 answer

0

You can, before saving - in the controller, check if the fields are empty and do not save if they are this way.

Or you can use the following expression in your model-parent:

  accepts_nested_attributes_for :model-child, allow_destroy: true, reject_if: lambda {|attributes| attributes['name'].blank?}

Anything post your controller and your models that I can see if I have knowledge to help you. I'm a newbie yet.

    
15.11.2016 / 15:17