Forum: Ruby on Rails How to add add child items to parent via parent show view?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Gunt (Guest)
on 2006-04-05 08:19
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?
Adam B. (Guest)
on 2006-04-07 03:04
> 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:

<a href="#" onclick="Element.show('add_comment')">Add New</a>

<div add_comment style="display:none">
#comment form
</div>

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

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:

<div add_comment style="display:none"> to:

<div add_comment <% unless @reloadstyle %>="display:none"<% end %>>

> 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?

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

-Adam
Adam B. (Guest)
on 2006-04-07 03:05
Ack. I messed up this line:

<div add_comment <% unless @reload %> style="display:none"<% end %>>
This topic is locked and can not be replied to.