I'm adding comments to my blog, which is running on Rails code that I wrote myself. So, I have Story and Comment: class Story < ActiveRecord::Base has_many :comments end class Comment < ActiveRecord::Base belongs_to :story, :counter_cache => true validates_length_of :author, :maximum => 25, :message => "Should be shorter" end That looks okay to me so far. Now, here's the problem I'm having and I need some help to solve it. I've added a form to the action that displays the story plus all comments associated with it. That form posts to an action in the controller: def add_comment @article = Story.find(@params[:story_id]) begin @article.comments.create!(@params[:new_comment]) rescue ActiveRecord::RecordInvalid # We have an invalid record, so let's make sure the user knows about # not sure what goes here to force the errors to show up in another action # so this is where I would put some sort of redirect end # Everything went well, so let's go back to the article redirect_to :action => 'article', :id => @params[:story_id] end So basically, I want to trap the exception generated by 'validates_length_of :author' and pass that error when I redirect back to my 'article' action. The idea is to send them back to the form, with their posting intact and report the error message. So, what's the best way to handle this? Do I need to create a seperate RHTML page just for the add_comment action or can I bend Rails to my will here?
on 2005-11-24 17:22