Using Aggregations in HTML forms?


#1

I am slightly struggling to get a basic aggregation setup

For example I have a column in my database called “email” which stores
an email address. For various reasons I want the form on screen to be
like “@domain.com”, ie the domain part is already
fixed - the username is changeable

So I create a class called Email which picks the two pieces apart.

class Email
attr_reader :name
attr_reader :domain

def initialise(email)
    @name = 'abc'
    #[@name,@domain] = email_addr.split(/@/)
end

def email
    return @name + '@' + @domain
end

end

Add a “composed_of :email” to my main class.

However, when I try to add a field to my form like this:
<%= text_field ‘mailbox’, ‘email.name’ %>

I get an error:

|undefined method `email.name’ for #Mailbox:0x37a1eb8|

What am I missing?

Presumably this now complicates the Edit action because you can’t just
assign the post results to the original object, instead you need to pick
apart some of the results, turn those into a new object and assign them
specifically to the correct fields? Are there some common idioms which
are useful here?

Is there a better way to do this? Seems to me that I might just create
attrs email_name and email_name= and then I can connect them directly to
the field in the form? Is this a bad idea?

Ed W