Forum: Ruby on Rails Using Aggregations in HTML forms?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
443a308e12ba2a3082fb6170ca94c2ef?d=identicon&s=25 Ed Wg (ewildgoose)
on 2005-12-20 17: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>@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
This topic is locked and can not be replied to.