Yeah,

I agree...

We recently completed a project in rails that had a couple of forms  
that had the "mother" model, 7 or 8 AJAX-controlled add/removeable  
children models, but not only that.... we also had grandchildren  
models in there too LOL (think many to many join table type  
associations where there is data stored on the join). And I think we  
also had one level below that.

in the end we wrote a piece of code to handle the whole lot - as many  
levels as you like... it took a LONG time, and two of us to complete  
it, and a lot of debugging.

Then, after that, Rails came out with this feature.... lol ... just  
when we'd finished it.

They didn't solve all the problems, though...

The problem has one main question in it:

"Do you want to save deletions/additions/changes AFTER or DURING  
editing?"

We chose the user-preferred view, which is that you can always press  
cancel to get back to the state you had before you made any  
additions / deletions or changes.

The "easy for rails/merb" way would have been make additions and  
deletions happen immediately, but changes happen when you press the  
save/update button.

It's interesting, isn't it?

I think perhaps we should roll this functionality (which also handled  
file uploads as well) into the current Rails source and submit it as a  
patch/feature request.

The sad thing about it, is that if we do this, many programmers will  
miss out on how complex and difficult this problem is. If the code is  
even slightly wrong, large chunks of data can be lost when saving.

Julian.

On 16/04/2009, at 5:01 AM, MarkMT wrote:

>
> This is a pretty tricky problem and it looks like it's the same as one
> I faced on a Rails project last year (actually my first Rails project
> and first experience with Ruby). I ran into a couple of issues. One is
> that you have to ensure that when you submit the form, back in the
> controller you're able to associate the fields for the child objects
> (the assignments in your case) with the correct parents (categories).
>
> I think the solution I used was pretty nasty and I won't describe it
> in detail, except to say that I used the bracket notation in
> fields_for with the :index option on the fields themselves to attach a
> unique index string to each field identifying both the field itself
> and the parent to which it belonged. In the controller I was able to
> use this to piece things together. IIRC merb doesn't use the bracket
> notation on  fields_for, so you would have to do this a little more
> manually, but in any case I'm not sure that the strategy as a whole is
> all that smart. I have more recently created nested forms in merb
> where I used appropriate choice of the :name attributes on the fields
> to cause the form data to be submitted as a nested hash, rather than
> having to build this structure in the controller.
>
> The second problem, which is also a little tricky, is how to
> dynamically add and remove fields. I actually found a blog article
> some place that helped me a lot with this - I wish I had it on hand to
> refer you to - I'll see if I can dig it out, but let me see if I can
> give you the gist...
>
> I used a link to send an ajax request to insert a new set of fields
> into the form. As you would expect the request has to tell the view
> where on the page to put the new div and also has to attach a unique
> id attribute to the new div to enable it to be subsequently deleted if
> required. The latter depends on the existing form structure. This
> means that the "add fields" link is itself actually dynamic, depending
> on how many sets of fields are currently on the page. So the ajax
> request that inserts the fields also has to replace the code for the
> link that sends the ajax request, so that it will do the right thing
> the next time it is invoked i.e. attach a different id to the next new
> div.
>
> Mark.
>
>
> cool wrote:
>> Guys,
>>          I got a big problem, badly i need a help in this issue.
>>
>> class Assignment < ActiveRecord::Base
>>    belongs_to :category
>> end
>>
>> class Category < ActiveRecord::Base
>>
>>    belongs_to :report
>>    has_many :assignments
>>
>> end
>>
>> The above are two models.
>>
>>   def new
>>       @category = Category.new
>>     #...@assignment = Assignment.new
>>     5.times { @category.assignments.build }
>> end
>>
>> Here is the view
>>
>>           <div id="idone">
>>                    <%= fields_for @category do %>
>>
>>                        <ul>
>>                            <li><label>Category(optional)</label><li>
>>                            <li><%= text_field  :category_name %></li>
>>                            <% for assignment in @category.assignments
>> %>
>>                                     <%= fields_for @assignment do %>
>>                                         <li>
>>                                             <%= text_field  :name =>
>> "name[]", :class => "short" %>
>>                                             <%= text_field  :name =>
>> "date[]", :class => "short apart" %>
>>                                             <%= text_field  :name =>
>> "max_point[]", :class => "short apart" %>
>>                                         </li>
>>                                     <% end =%>
>>                             <% end %>
>>                         </ul>
>>                     <% end =%>
>>              </div>
>>
>> i have a link called add another category. If i click that link it
>> should add all the content (complete div)
>>
>> how i can do that in java script or merb
>>
>> it is becoming very tough for me
>> can u help me guys..
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"merb" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/merb?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to