How to handle errors - HELP


#1

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!


#2

Hi !

2006/3/17, rh removed_email_address@domain.invalid:

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 !