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