How to add add child items to parent via parent show view?


#1

I want to know the standard/easiest way to add items to a parent?

Example: comment added to post

I would like this to happen in the â??showâ?? view of post using a form that
is shown when the user clicks the â??add newâ?? button/link. I would also
like validation on the child form with the validation errors appearing
in the post showâ?? view.

Two problems are presented:

  1. When the user clicks the add new button, the form needs to appear.
    This suggests that it is in the page when it is loaded and is simply
    shown when the â??add newâ?? link is clicked. When the error message(s)
    appear on the page, the â??showâ?? view will be refreshed and the form wonâ??t
    be shown â?? how to avoid this?

  2. How to deal with the error messages and the problem of nil.errors or
    validation and errors in general. How do I get them to display in the
    post â??showâ?? view when validation fails and not get the nil.errors
    problem when the post is simply being viewed?

The â??showâ?? view allows the adding of comments with validation and the
validation errors and shown in the â??showâ?? view of the post page.

I am stumped. Completely stumped. I have tried a few methods to get this
working and do not want to post any code since none of them worked and
want to know the best/easiest/common/preferred/actual (take your pick)
method of doing this.

It seems like such an easy thing to do, but I just cannot get this to
work. How would you suggest doing this?


#2
  1. When the user clicks the add new button, the form needs to appear.
    This suggests that it is in the page when it is loaded and is simply
    shown when the â??add newâ?? link is clicked. When the error message(s)
    appear on the page, the â??showâ?? view will be refreshed and the form wonâ??t
    be shown â?? how to avoid this?

By your description I assume you don’t have a method for hiding it?
That’s pretty simple:

Add New

#comment form

Here’s how I would deal with reloading after errors. There may be a
better way, if there is I hope someone posts it. :slight_smile:

In the ‘show’ action code add a new variable:

@reload = false

In the “add comment” action code add a line right before render :action
=> ‘show’:

@reload = true

Then in the code I showed above, change:

to:
="display:none"<% end %>>
  1. How to deal with the error messages and the problem of nil.errors or
    validation and errors in general. How do I get them to display in the
    post â??showâ?? view when validation fails and not get the nil.errors
    problem when the post is simply being viewed?

I’m not sure about this one, but you might try adding <% if errors %>
before the error display code.

-Adam


#3

Ack. I messed up this line:

style="display:none"<% end %>>