Forum: Ruby on Rails How to handle errors - HELP

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.
1e2dd87a31c5d3e07adc9efa375f4dc4?d=identicon&s=25 rh (Guest)
on 2006-03-17 14:18
I'm having trouble with my Ajax comments, but only when someone clicks
the submit button when there's no comment.  How can I handle this in my
code?

Do I use "rescue nil" or something?

Here's my controller:


def comments
    content = Content.find(params[:id])
    @comment = Comment.new(params[:comment])
    content.comments << @comment
    content.save
    @comment.save
    @comment_count = Comment.count("content_id=#{params[:id]}")
    render_without_layout
end



If the comment is blank, then do I want to rescue the nil, then "render
:text => 'Please fill out the fields.'"?  Then in my remote form
options, on the :update, I could have:

:update => {:success => 'the_right_div',:failure => 'error_div'}

????

This is how I interpret the overall process, but I don't know the
correct syntax on how to do this with the resue, and what I check for.

I just want nothing to happen when the user clicks the "submit comment"
button when there's nothing to submit.

Please HELP!

Thanks in advance for any tips - this forum has really helped me out!
3dd4b52a0946bd698b1d1635a46ea3a3?d=identicon&s=25 François Beausoleil (fbeausoleil)
on 2006-03-17 15:31
(Received via mailing list)
Hi !

2006/3/17, rh <rheath@ircwv.com>:
> def comments
>     content = Content.find(params[:id])
>     @comment = Comment.new(params[:comment])
>     content.comments << @comment
>     content.save
>     @comment.save
>     @comment_count = Comment.count("content_id=#{params[:id]}")
>     render_without_layout
> end

#save returns true if it was able to save.  So, you need to do
something like this instead:

if @comment.save then
  # what you have above
else
  # render another view, saying there was one or more errors
end

Hope that helps !
This topic is locked and can not be replied to.