Virtual Attributes & Rails -
I have a phone model for the phone number in my application that are made as follows:
T.column: number ,: string t.references: phoneable ,: polymorphic => True
I want to restrict the number of format 317.555.5555x234, so I am creating a form with four boxes (area code, 3 digits, 4 digits, extras):
- form_for | User you User_forms | -user_form.fields_for: calling | Phone | = Phone.text_field: area_code = phone.text_field: first_three_digits etc ...
I am assuming a virtual attribute that will be the route to go (A La Relentants ep16), but not sure How to collect "number" from 4 different text_fields
I think I have to do something like this:
def full_number = (phone) self.number "+ + first" - "+ +" + "" + + "Second +" + + + ". + + Extension of the extension
But I have uncertainty about how to collect numbers from form envelopes. Any ideas? < / P>
I normally do this as first_ervice:
First_save: update_phone_number def update_phone_number self.phone_number = [area_code, first_three_digits, second_four_digits, extension] .reject (& amp;: blank?). Join ('.') A
First of all I have some assumptions:
validates_presence_of: area_code,: first_three_digits ,: second_four_digits validates_format_of: area_code, with => / \ D {3} / validates_format_of: second_four_digits ,: with = & gt; / \ D {4} / validates_format_of: extension, with = = / \ D {0/6} / ,: allow_blank = & gt; True
This is just to ensure that you get valid data in your phone number and do not make any errors before saving. I have also been assumed that you allow the expansion to be empty, but it easily changes.
EDIT: You want attr_accessors for different segments of the phone number:
attr_accessor: area_code,: first_three_digits,: second_four_digits ,: extension
Comments
Post a Comment