Better way to handle has-n child models on a form? (Merb+DM)

1 view
Skip to first unread message

George

unread,
Nov 17, 2009, 5:10:53 AM11/17/09
to DataMapper
If a Client model has-n addresses, what's the best way to handle these
on a form?

class Client
...
has n, :client_addresses
has n, :addresses, :through => :client_addresses
...
end

In a Client edit form we have something like this:
<% client.addresses.each do |address| %>
<div>
<%= fields_for address do %>
<%= text_field :address1, :label=>"Number/name" %>
<%= text_field :address2, :label=>"Locality" %>
...etc...
<% end =%>
</div>
<% end %>

That renders each html address field with names like "address
[address1]" so I would expect the server side to receive those as
arrays of 0-or-more values. Looking at the output it seems that merb
only sees one value for each field.
Is this the right approach? How should we handle the submitted address
fields server side?
I suppose I could parse the params object manually but that is not
very reusable.

Many thanks
George

Martin Gamsjaeger

unread,
Nov 17, 2009, 7:03:15 AM11/17/09
to datam...@googlegroups.com
George,

You may want to give
http://github.com/snusnu/dm-accepts_nested_attributes a try. It should
take care of the DM side of things. I'm not entirely sure how merb's
#field_for plays with it atm, but I'd be glad to work with you (and
accept patches :) to make merb(-helpers) and dana (short for
dm-accepts_nested_attributes) play together nicely.

cheers
snusnu
> --
>
> You received this message because you are subscribed to the Google Groups "DataMapper" group.
> To post to this group, send email to datam...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/datamapper?hl=.
>
>
>

MarkMT

unread,
Nov 17, 2009, 7:20:05 AM11/17/09
to DataMapper
George,

I haven't looked too deeply at your situation, but if you're using
merb, I have a feeling you may be running into a similar issue to one
I had a while back. In my case my conclusion was that there were some
limitations in merb's form helpers that prevented them from
constructing the input arrays in the way I had expected based on my
experience with Rails. However the actual parsing of the inputs in the
merb request handler is fine. My work-around was to use the :name
option in the field helpers to explicitly construct the inputs in the
form I needed. This thread has a few details -

http://groups.google.com/group/merb/browse_thread/thread/d1a192fbe2919497/9b6aef5c470f0b2b?#9b6aef5c470f0b2b

Mark.

trung

unread,
Nov 22, 2009, 8:42:26 PM11/22/09
to DataMapper
I've been using this plugin and I rant into two very big bugs.

1. If the child model fails validation, the parent.new_record? method
still return true. It should be false.

2. If the child model fails validation, the parent after save and
after create callback still run. Parent's callback should halt.

Thank you.

On Nov 17, 4:03 am, Martin Gamsjaeger <gamsnj...@gmail.com> wrote:
> George,
>
> You may want to givehttp://github.com/snusnu/dm-accepts_nested_attributesa try. It should

Martin Gamsjaeger

unread,
Nov 23, 2009, 10:49:18 AM11/23/09
to datam...@googlegroups.com
I will try to look into this issues (with
dm-accepts_nested_attributes) and see if I can come up with a fix
soon. However, I'm currently rather busy. Of course I'd be glad to
accept patches!

cheers
snusnu
> To unsubscribe from this group, send email to datamapper+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages