Add multiple phones to a telephone_field in Rails

Asked

Viewed 191 times

0

I’m doing a customer registration here to shop and it turns out that some customers have more than 2 phone numbers the system already works with two telephone fields a landline and a mobile phone :

<%= f.label :telcelular , "Tel. Celular" %>
<%= f.telephone_field :telcelular %>

<%= f.label :telfixo , "Tel. Fixo" %>
<%= f.telephone_field :telfixo %>

There is no way I create a field for each phone, since I do not know the amount of phone numbers that each customer has, there is some possibility to save all phones only in one field?

  • 2

    The most correct would be to create a table telefones containing a customer reference (cliente_id). You can put everything in a form using Nested Attributes (1 2 3), but I never tried to use it. The easiest thing is to create a separate form just to add the phones.

  • Using Nested Attributes is easy to do. And you can leave in the form a button to go adding fields for phones if necessary. You can take a look at this example http://railscasts.com/episodes/196-nested-model-form-part-1

1 answer

0

A suggestion that wouldn’t affect much of what already exists would be to serialize these fields. postgress for example allows you to store an array in it, and Rails has functions to serialize that array.

The only problem I believe would be to serialize the objects that are already in the bank. At the most so you could have as many phones as you want in one field. One change to the model, controller and Javascript would be enough to print more fields in the view...

Browser other questions tagged

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