[NEWBIE] Error redirection

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

On 29 Sep 2008, at 09:31, FM wrote:

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.

Typically one does that by rendering the edit/create form again rather
than redirecting.

Fred

On Sep 29, 10:58 am, Frederick C. [email protected]
wrote:

On 29 Sep 2008, at 09:31, FM wrote:

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.

Typically one does that by rendering the edit/create form again rather
than redirecting.

Thank for your help.
So basically I created a method that fills all the needed variables
and I call it from both the show_by_id and create_comment actions.

However, now I have another question :slight_smile:
The comment form is located at the end of the page. So basically, when
the user submit a comment, I would like the page to be scrolled down
to the ‘comment_form’ anchor (i.e., where flashes are currently
displayed)

I tried with this:
form_for(@comment, :url => { :anchor => ‘comment_form’})
but doesn’t seem to work.

I think that the only solution to this is to put the flash zone at the
beginning of the page, but maybe that there is some technique to get
this behavior*.

Any other idea? :slight_smile:

Thanks again,
Fabio

  • Actually this behavior would be handy to scroll the page to the
    inserted comment in order to present it to the user for giving him a
    success feedback.

On Sep 29, 12:31 pm, Frederick C. [email protected]
wrote:

If you are redirecting the user somewhere upon creation of the comment
then that redirect should specify the anchor.

As you previously suggested I am not using redirection anymore, but I
render the whole post page.

My action basically does this: http://pastie.org/281283
And this is my ‘show’ view: http://pastie.org/281286

So I need to show the user the page starting from the ‘comment_form’
anchor as a result of the rendering.

Thanks,
Fabio

On Sep 29, 11:14 am, FM [email protected] wrote:

I tried with this:
form_for(@comment, :url => { :anchor => ‘comment_form’})
but doesn’t seem to work.

I think that the only solution to this is to put the flash zone at the
beginning of the page, but maybe that there is some technique to get
this behavior*.

If you are redirecting the user somewhere upon creation of the comment
then that redirect should specify the anchor.

Fred

On Sep 29, 12:11 pm, FM [email protected] wrote:

My action basically does this:http://pastie.org/281283
And this is my ‘show’ view:http://pastie.org/281286

So I need to show the user the page starting from the ‘comment_form’
anchor as a result of the rendering.

You’ve still got a redirect in the success case.
redirect_to(:back, :anchor => ‘comment_form’) }

won’t actually use that anchor at all (the :back option wins).

Also check that the html of the document looks right.

Fred