Using Aggregations in HTML forms?

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 “”, 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(/@/)

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


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’, ‘’ %>

I get an error:

|undefined method `’ 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs