Forum: Ruby on Rails Using Aggregations in HTML forms?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Ed W. (Guest)
on 2005-12-20 18:34
(Received via mailing list)
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 "<enter email name here>", 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 topic is locked and can not be replied to.