Class Person > Legal person > Natural person. Rails 4

Asked

Viewed 264 times

-1

I’m developing a APP in Rails, and I came up with the following question regarding the class Person.

The structure of these classes are 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 a radio_button to select the type of person to be registered(Fisica ou Juridica), but I am in doubt how to create the form fields. In this case the relationship is has_one from Pessoa to PessoaFisica/Juridica, how I would create this _form to have the fields both from the table of PessoaFisica as of PessoaJuridica and change them depending on the selection of radio_button ?

I thought of creating two _forms one for the individual, and the other for the legal and view with a style display: none; and show them with jQuery when selected on radio_button, that is valid ? And how would the controller When saving the registration, how will I identify when creating, if you are a legal person ? and in which Model save ?

Thanks in advance, Hugs.

EDIT

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

1 answer

0

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

Or maybe 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 I can see if I know how to help you. I’m new yet.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.