Forum: Ruby on Rails Redirect_to Vs Render_component

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Srividya S. (Guest)
on 2009-02-13 14:31
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.
Mark Reginald J. (Guest)
on 2009-02-14 13:07
(Received via mailing list)
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
Srividya S. (Guest)
on 2009-02-14 13:26
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
Bharat R. (Guest)
on 2009-02-15 15:59
(Received via mailing list)
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
Srividya S. (Guest)
on 2009-02-15 18:15
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
Bharat R. (Guest)
on 2009-02-16 00:24
1.  place where you want to save your hash:

flash[:my_hash] = some_hash

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

this_hash = flash[:my_hash]

Now this_hash has your saved data.

Bharat
Srividya S. (Guest)
on 2009-02-23 08:23
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
>
> 2.  place where you want retrieve your saved hash in flash:
>
> this_hash = flash[:my_hash]
>
> Now this_hash has your saved data.
>
> Bharat
Srividya S. (Guest)
on 2009-02-25 16:43
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
>>
>> 2.  place where you want retrieve your saved hash in flash:
>>
>> this_hash = flash[:my_hash]
>>
>> Now this_hash has your saved data.
>>
>> Bharat
This topic is locked and can not be replied to.