Nested Document Forms

瀏覽次數:237 次
跳到第一則未讀訊息

jmcervera

未讀,
2010年1月12日 清晨6:11:232010/1/12
收件者:MongoMapper
Hello,

Is there a way to specify something like accept_nested attributes_for
for embedded documents, and easy design a Nested Document Form as in
ActiveRecord?

Anybody knows if thIs this planned for the future?
Thanks

John Nunemaker

未讀,
2010年1月12日 上午10:56:222010/1/12
收件者:mongo...@googlegroups.com
Nope and nope. It is really nit that hard with attr_accessor's.

> --
> You received this message because you are subscribed to the Google
> Groups "MongoMapper" group.
> For more options, visit this group at
> http://groups.google.com/group/mongomapper?hl=en?hl=en

John Nunemaker

未讀,
2010年1月12日 上午10:56:412010/1/12
收件者:mongo...@googlegroups.com
I'm nit opposed. Just don't have plans.

On Jan 12, 2010, at 6:11 AM, jmcervera <juanma...@gmail.com> wrote:

Chris Hanks

未讀,
2010年1月12日 中午12:25:232010/1/12
收件者:MongoMapper
I've been having a lot of problems with this recently. One solution
I've found is to include :index => nil on your fields_for
declarations, but this wreaks havoc with radio buttons (multiple
children will all share the same options), so in my situation it's not
really usable.

If anyone has a good generic solution for handling a document and many
embedded documents in a single form, though, I'd really like to see
it. I've been hacking around it in my controllers, and it's been very
ugly.

Lance Carlson

未讀,
2010年1月12日 下午1:37:292010/1/12
收件者:mongo...@googlegroups.com
to get nested attributes to work in datamapper, I had to do this: (it
assumes you're using rails.. maybe you can port it to work with
mongomapper)

def accepts_nested_attributes_for(*args)
arg = *args.pop
target_model = arg.to_s.classify
is_many = !arg.to_s.grep(/[^s]s$/).empty?

class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{arg}_attributes
instance_variable_get("@#{arg}_attributes")
end

def #{arg}_attributes=(value)
new_value = []
value.each {|key, attrs|
new_value << #{target_model}.new(attrs)
}

new_value = new_value.first unless #{is_many}

send("#{arg}=", new_value)
end
RUBY
accepts_nested_attributes_for(*args) unless args.empty?
end

Zef

未讀,
2010年1月12日 下午3:51:412010/1/12
收件者:MongoMapper
I'm actually working on this... my implementation (based on
ActiveRecord) is definitely not done, but it works basically right
now.

A warning though: if you mark a related document for destruction (by
assigning "_destroy" => true), it will be destroyed even if the
document is not saved. This is only for associated documents (not
embedded docs). I have an idea of how to change this, but it will take
some more work to get there.

Feel free to use if you want, but no guarantees... Tests aren't
complete (and one is failing now), but that's why I haven't released
this publicly yet.

http://gist.github.com/275594

Hope this helps!

jmcervera

未讀,
2010年1月18日 中午12:00:552010/1/18
收件者:MongoMapper
Thanks Zef

Based in what you've done, I have ported the activerecord version

https://gist.github.com/d313817a57fd079e4df7

It works pretty well for me, I include MongoMapper::NestedAttributes
in every Document and EmbeddedDocument that need nested attributes.
I think it would be nice that something like this will be merged in
MongoMapper and have the functionality already included.

Juanma

Chris Hanks

未讀,
2010年1月18日 晚上8:49:542010/1/18
收件者:MongoMapper
Wow, awesome work all around. I'm using Juanma's gist right now and
it's working great.

I'd really like to see this in MongoMapper core as well.

Thanks a lot!

Manuel

未讀,
2010年1月19日 清晨5:43:502010/1/19
收件者:MongoMapper
I already have a nested form for my embedded-documents, and it works
great with :index => nil.

But somehow @parent.update_attributes(params[:parent-with-embedde-
docs] doesn't work....

It works for the parent object, the embedded-documents get updated
too, but every value that isn't sent with the form is overrridden with
nil....
But creating a new Record like this works...

Maybe somebody else has an idea.

Regards,
Manuel

Manuel

未讀,
2010年1月20日 凌晨3:50:072010/1/20
收件者:MongoMapper
It's pretty obvious that a new record creation works like that ^^

Is update_attributes designed to also update embedded_documents?
Or do I have to overwrite update_attributes or use the patch from
Juanma?

I just thought that this works without a patch, because the creation
of a new record works perfect...

I use the latest master branch.

Thanks for every hint :-)

回覆所有人
回覆作者
轉寄
0 則新訊息