Gunt
2006-Apr-05 04:19 UTC
[Rails] How to add add child items to parent via parent show view?
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? -- Posted via http://www.ruby-forum.com/.
Adam Bloom
2006-Apr-06 23:04 UTC
[Rails] Re: How to add add child items to parent via parent show vie
> 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 -- Posted via http://www.ruby-forum.com/.