# Redirect\_to Vs Render\_component

**URL:** <https://www.ruby-forum.com/t/redirect-to-vs-render-component/160256>\
**Category:** Rails\
**Created:** [February 13, 2009, 1:31pm UTC](https://www.ruby-forum.com/t/redirect-to-vs-render-component/160256 "2009-02-13T13:31:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![srividya\_sharma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/57b2e6/32.png) [@srividya\_sharma](https://www.ruby-forum.com/u/srividya_sharma)\
**Post date:** [February 13, 2009, 1:31pm UTC](https://www.ruby-forum.com/t/redirect-to-vs-render-component/160256/1 "2009-02-13T13:31:01Z")

</div>

Hello  
I am using Rails 2.2.2.  
Here is my problem:  
I have a controller action which will receive several parameters,  
validate them, process them and create a new set of parameters out of  
them.  
Then, this controller should delegate the task to another controller  
action based on some values and pass these processed parameters to it. I  
have stored the processed parameters in a hash. (there are many of them)

I could only see two options to achieve this:

1. Use redirect\_to and pass each parameter as a string.
2. Use render\_component and pass the parameters as a hash.

I have seen that render\_component is not recommended in most cases. Is  
it recommended solution for this case? I think render\_component slows  
down the request also.  
Which option is right for me?  
Are there any other ways to achieve this? Please guide me.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![srividya\_sharma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/57b2e6/32.png) [@srividya\_sharma](https://www.ruby-forum.com/u/srividya_sharma)\
**Post date:** [February 14, 2009, 12:07pm UTC](https://www.ruby-forum.com/t/redirect-to-vs-render-component/160256/2 "2009-02-14T12:07:24Z")

</div>

Srividya Sharma wrote:

> I could only see two options to achieve this:
> 
> 1. Use redirect\_to and pass each parameter as a string.
> 2. Use render\_component and pass the parameters as a hash.
> 
> I have seen that render\_component is not recommended in most cases. Is  
> it recommended solution for this case? I think render\_component slows  
> down the request also.  
> Which option is right for me?  
> Are there any other ways to achieve this? Please guide me.

If you don’t want the browser url to change, you can just call  
the other action like a method, passing the parameters in either  
an instance variable or a method parameter (that’s default nil  
to allow it to be called externally). Or are you messing with  
params directly?

This is assuming the other method is in the same controller.  
If it isn’t, you’ll have to make the other method available  
within the first controller, either through inheritance or  
mix-ins.

–  
Rails Wheels - Find Plugins, List & Sell Plugins -  
[http://railswheels.com](http://railswheels.com)

---

<div class="post-metadata">

**Author:** ![srividya\_sharma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/57b2e6/32.png) [@srividya\_sharma](https://www.ruby-forum.com/u/srividya_sharma)\
**Post date:** [February 14, 2009, 12:26pm UTC](https://www.ruby-forum.com/t/redirect-to-vs-render-component/160256/3 "2009-02-14T12:26:14Z")

</div>

Nice to see a response. The other method is in another controller. And  
the other method needs to use send\_data. (basically write a response)  
So I cannot use a simple class, include that class in the first  
controller.  
since the other method has to write out a response, it needs to be in  
another controller.(I hope I am right about this)

In the first controller, the request parameters are processed and these  
processed parameters need to be passed to the other method in the other  
controller.

Mark Reginald J. wrote:

> Srividya Sharma wrote:
> 
> > I could only see two options to achieve this:
> > 
> > 1. Use redirect\_to and pass each parameter as a string.
> > 2. Use render\_component and pass the parameters as a hash.
> > 
> > I have seen that render\_component is not recommended in most cases. Is  
> > it recommended solution for this case? I think render\_component slows  
> > down the request also.  
> > Which option is right for me?  
> > Are there any other ways to achieve this? Please guide me.
> 
> If you don’t want the browser url to change, you can just call  
> the other action like a method, passing the parameters in either  
> an instance variable or a method parameter (that’s default nil  
> to allow it to be called externally). Or are you messing with  
> params directly?
> 
> This is assuming the other method is in the same controller.  
> If it isn’t, you’ll have to make the other method available  
> within the first controller, either through inheritance or  
> mix-ins.
> 
> –  
> Rails Wheels - Find Plugins, List & Sell Plugins -  
> [http://railswheels.com](http://railswheels.com)

---

<div class="post-metadata">

**Author:** ![srividya\_sharma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/57b2e6/32.png) [@srividya\_sharma](https://www.ruby-forum.com/u/srividya_sharma)\
**Post date:** [February 15, 2009, 2:59pm UTC](https://www.ruby-forum.com/t/redirect-to-vs-render-component/160256/4 "2009-02-15T14:59:36Z")

</div>

Srividya,  
The way I have been doing this rather quick and dirty but it works.  
Basically, you want to communicate between two HTTP requests. One of  
the most useful facilities in Rails is flash which is a special member  
of the session hash. It is special in the sense that it is  
temporary. By default, it will retain the values stored in it across  
two request and then it discards them automatically. Easy solution,  
just stuff the hash in the flash which itself is a hash anyway and  
retrieve it in any action that you desire. It has worked very well  
for me.  
Bharat

---

<div class="post-metadata">

**Author:** ![srividya\_sharma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/57b2e6/32.png) [@srividya\_sharma](https://www.ruby-forum.com/u/srividya_sharma)\
**Post date:** [February 15, 2009, 11:24pm UTC](https://www.ruby-forum.com/t/redirect-to-vs-render-component/160256/5 "2009-02-15T23:24:21Z")

</div>

1. place where you want to save your hash:

flash[:my\_hash] = some\_hash

1. place where you want retrieve your saved hash in flash:

this\_hash = flash[:my\_hash]

Now this\_hash has your saved data.

Bharat

---

<div class="post-metadata">

**Author:** ![srividya\_sharma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/57b2e6/32.png) [@srividya\_sharma](https://www.ruby-forum.com/u/srividya_sharma)\
**Post date:** [February 23, 2009, 7:23am UTC](https://www.ruby-forum.com/t/redirect-to-vs-render-component/160256/6 "2009-02-23T07:23:58Z")

</div>

I tried render\_component, it seems to be slow.  
Thanks a lot. I will give this a try.

Bharat R. wrote:

> 1. place where you want to save your hash:
> 
> flash[:my\_hash] = some\_hash
> 
> 1. place where you want retrieve your saved hash in flash:
> 
> this\_hash = flash[:my\_hash]
> 
> Now this\_hash has your saved data.
> 
> Bharat

---

<div class="post-metadata">

**Author:** ![srividya\_sharma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/57b2e6/32.png) [@srividya\_sharma](https://www.ruby-forum.com/u/srividya_sharma)\
**Post date:** [February 25, 2009, 3:43pm UTC](https://www.ruby-forum.com/t/redirect-to-vs-render-component/160256/7 "2009-02-25T15:43:24Z")

</div>

I tried using the flash message, but the data that I need to pass is  
huge. so, I get a cookie overflow error.  
This technique did not work out for me. Is there anything else I could  
use?  
I just recalled that render\_component is deprecated in 2.2.2 and a  
plug-in has to be used to get the same functionality.  
Is there any way out?

Srividya Sharma wrote:

> I tried render\_component, it seems to be slow.  
> Thanks a lot. I will give this a try.
> 
> Bharat R. wrote:
> 
> > 1. place where you want to save your hash:
> > 
> > flash[:my\_hash] = some\_hash
> > 
> > 1. place where you want retrieve your saved hash in flash:
> > 
> > this\_hash = flash[:my\_hash]
> > 
> > Now this\_hash has your saved data.
> > 
> > Bharat

---

<div class="post-metadata">

**Author:** ![srividya\_sharma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/57b2e6/32.png) [@srividya\_sharma](https://www.ruby-forum.com/u/srividya_sharma)\
**Post date:** [February 15, 2009, 5:15pm UTC](https://www.ruby-forum.com/t/redirect-to-vs-render-component/160256/8 "2009-02-15T17:15:26Z")

</div>

So, I should use redirect\_to and use flash for passing the hash.  
Can you give me code snippet?

Thanks a lot.

Bharat R. wrote:

> Srividya,  
> The way I have been doing this rather quick and dirty but it works.  
> Basically, you want to communicate between two HTTP requests. One of  
> the most useful facilities in Rails is flash which is a special member  
> of the session hash. It is special in the sense that it is  
> temporary. By default, it will retain the values stored in it across  
> two request and then it discards them automatically. Easy solution,  
> just stuff the hash in the flash which itself is a hash anyway and  
> retrieve it in any action that you desire. It has worked very well  
> for me.  
> Bharat
