Dear all,
I am learning Rails and to do so I am writing a blogging system. So far everything is going well but now I have a problem and I don't know how to solve it.
Basically the "article" page presents the current post, all the comments associated with it and a form for adding a new comment. So, before yielding to the template rendering I populate the instance variable with actual values:
def show_by_id @post = Post.find(params[:id]) #The actual article @comments = Comment.find(:all, :order => 'created_at ASC', :conditions => ['post_id = ?', @post]) #All the comments @comment = Comment.new #Used for building the form for posting a new comment. ...
(Note: this action is associated to the GET '/blog/post/:id' route)
So far so good.
Now I have to handle the comment posting. The controller's method for doing this is pretty standard:
def create_comment @comment = Comment.new(params[:comment]) respond_to do |format| ...Captcha validation... if @comment.save ... ...
(Note: this action is associated to the POST '/blog/post/:id' route)
I included in the comment form also a simple captcha (basically a math- formula where the user should write the result for) In case of error I would like to redirect to the article page and display a notice, so I do a redirect_to and the flash is displayed and everything is fine.
Now here it is my problem: when there is an error I would like the redirect_to preserve the comment data entered by the user. Right now, after the error (for example by failing the captcha) the user is redirected to the article page and presented with an empty form (that's obvious because the show_by_id action does a @comment = Comment.new). But I would like the form to be re-filled with the previously written data and to spot the where is the problem.
To do so, I should in some way pass the comment data to the show_by_id action so that the @comment variable can be initialized with these data and presented in the form.
Any idea about how can I do this?
Thank you, Fabio