# Help redirect\_to funciton

**URL:** <https://www.ruby-forum.com/t/help-redirect-to-funciton/134627>\
**Category:** Rails\
**Created:** [April 13, 2008, 4:29am UTC](https://www.ruby-forum.com/t/help-redirect-to-funciton/134627 "2008-04-13T04:29:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![duke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e8c25b/32.png) [@duke](https://www.ruby-forum.com/u/duke)\
**Post date:** [April 13, 2008, 4:29am UTC](https://www.ruby-forum.com/t/help-redirect-to-funciton/134627/1 "2008-04-13T04:29:02Z")

</div>

I used scaffold to generate a product  
The scaffold created some code in the product controller  
def create  
@product = Product.new(params[:product])

```
respond_to do |format|
  if @product.save
    flash[:notice] = 'Product was successfully formcreated.'
    format.html { redirect_to(@product) }
    format.xml { render :xml => @product, :status

```

=\> :created, :location =\> @product }  
else  
format.html { render :action =\> “new” }  
format.xml { render :xml =\> @product.errors, :status  
=\> :unprocessable\_entity }  
end  
end  
end

format.html {redirect\_to(@product)}  
I could not understand the this line and since its does not contain  
any action.  
pls help in understanding the code.

---

<div class="post-metadata">

**Author:** ![duke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e8c25b/32.png) [@duke](https://www.ruby-forum.com/u/duke)\
**Post date:** [April 13, 2008, 3:27pm UTC](https://www.ruby-forum.com/t/help-redirect-to-funciton/134627/2 "2008-04-13T15:27:11Z")

</div>

On 13 Apr 2008, at 03:28, Duke wrote:

> ```
> format.xml { render :xml => @product, :status
> 
> ```
> 
> =\> :created, :location =\> @product }  
> else  
> format.html { render :action =\> “new” }  
> format.xml { render :xml =\> @product.errors, :status  
> =\> :unprocessable\_entity }  
> end  
> end  
> end
> 
> format.html {redirect\_to(@product)}  
> That says ‘if the client requested html, the redirect them to the url  
> that shows @product’. Because you’ve got your nice restful routing  
> redirect\_to knows that it should send you to /products/show/123  
> (assuming that the id of @product is 123)

Fred

---

<div class="post-metadata">

**Author:** ![duke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e8c25b/32.png) [@duke](https://www.ruby-forum.com/u/duke)\
**Post date:** [April 13, 2008, 3:27pm UTC](https://www.ruby-forum.com/t/help-redirect-to-funciton/134627/3 "2008-04-13T15:27:46Z")

</div>

Duke,

The respond\_to is a new feature of Rails 2 (It might have been in  
rails 1.2 at some point) and is intended to let you define different  
responses depending upon the “type” of the request. The most standard  
examples are:

- normal HTML request, you respond with a normal Rails response,  
usually a default render, another render or a redirect
- XML request, you respond with rendering XML, usually with the object  
or objects
- RJS request, you respond with an update block or with an RJS file,  
usually because of AJAX calls

So, when you have:

format.html { redirect\_to(@product) }

It’s saying: if the “type” of the request is a normal HTML request,  
then redirect to the URL for handling a specific product. This is the  
“show” action, by default, in REST-style routes. So, it’s the  
functional equivalent of:

redirect\_to :action =\> “show”, :controller =\> “products”, :id =\>  
@product.id

Also, the respond\_to is nice if you need it, but also unnecessary. If  
your app doesn’t need to support it, just take it off, which means  
that method could simply be:

def create  
@product = Product.new(params[:product])  
if @product.save  
flash[:notice] = ‘Product was successfully created.’  
redirect\_to(@product)  
else  
render :action =\> “new”  
end  
end

HTH!

-Danimal
