# \[NEWBIE\] Error redirection

**URL:** <https://www.ruby-forum.com/t/newbie-error-redirection/149746>\
**Category:** Rails\
**Created:** [September 29, 2008, 10:32am UTC](https://www.ruby-forum.com/t/newbie-error-redirection/149746 "2008-09-29T10:32:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![FMFMFM](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f08c70/32.png) [@FMFMFM](https://www.ruby-forum.com/u/FMFMFM)\
**Post date:** [September 29, 2008, 10:32am UTC](https://www.ruby-forum.com/t/newbie-error-redirection/149746/1 "2008-09-29T10:32:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![FMFMFM](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f08c70/32.png) [@FMFMFM](https://www.ruby-forum.com/u/FMFMFM)\
**Post date:** [September 29, 2008, 10:59am UTC](https://www.ruby-forum.com/t/newbie-error-redirection/149746/2 "2008-09-29T10:59:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![FMFMFM](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f08c70/32.png) [@FMFMFM](https://www.ruby-forum.com/u/FMFMFM)\
**Post date:** [September 29, 2008, 12:15pm UTC](https://www.ruby-forum.com/t/newbie-error-redirection/149746/3 "2008-09-29T12:15:01Z")

</div>

On Sep 29, 10:58 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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 🙂  
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? 🙂

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.

---

<div class="post-metadata">

**Author:** ![FMFMFM](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f08c70/32.png) [@FMFMFM](https://www.ruby-forum.com/u/FMFMFM)\
**Post date:** [September 29, 2008, 1:12pm UTC](https://www.ruby-forum.com/t/newbie-error-redirection/149746/4 "2008-09-29T13:12:33Z")

</div>

On Sep 29, 12:31 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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](http://pastie.org/281283)  
And this is my ‘show’ view: [http://pastie.org/281286](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

---

<div class="post-metadata">

**Author:** ![FMFMFM](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f08c70/32.png) [@FMFMFM](https://www.ruby-forum.com/u/FMFMFM)\
**Post date:** [September 29, 2008, 12:32pm UTC](https://www.ruby-forum.com/t/newbie-error-redirection/149746/5 "2008-09-29T12:32:35Z")

</div>

On Sep 29, 11:14 am, FM [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![FMFMFM](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f08c70/32.png) [@FMFMFM](https://www.ruby-forum.com/u/FMFMFM)\
**Post date:** [September 29, 2008, 2:10pm UTC](https://www.ruby-forum.com/t/newbie-error-redirection/149746/6 "2008-09-29T14:10:05Z")

</div>

On Sep 29, 12:11 pm, FM [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> My action basically does this:[http://pastie.org/281283](http://pastie.org/281283)  
> And this is my ‘show’ view:[http://pastie.org/281286](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
