Welcome to Working With Rails

 

Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
redirect_to vs. render_component
7 Posts
redirect_to vs. render_component

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 and contain lot of data)

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.

If I use redirect_to, I have to convert all the parameters present in a hash into several string parameters.

I have seen that render_component is not recommended in most cases. It is also deprecated in 2.2.2 . Are there any other ways to achieve this? I want to pass parameters hash to another controller's action. Please guide me.

Thanks in advance.

Hi,

if you can describe your objective more clearly in detail that will be a great helpful.

i.e. why you need such "two" action for this "one" process, or what do these two action stand for?

The first controller acts as a delegator. It gets a set of parameters, checks if they are in order- if some parameters are missing, it adds some default values to them and constructs a hash. It does some basic error checking and then based on one parameter, say "type" it has to forward the request to another controller. For example, if "type" is "Excel" it needs to forward the request to "ExcelController", if type is "CSV" it needs to forward the request to "CSVController", if type is PDF then "PDFController". Each of these controllers, will use this set of parameters (processed by the first controller) and generate the output of required type and use send_data to stream it. Of course, each of these controllers uses a number of .rb files as well.

Currently, I am using render_component in this way: controller_name = some prefix + type render_component :controller=>controller_name, :action=> some_action ,:params=>{:data=>parameters_hash} Since render_component has deprecated, I no longer want to use it. Please tell me an alternative.

Thanks

To begin with, it sounds like you have much too much logic in your controllers. Validation, error checking, setting default values, data manipulation: this is what models are for. Your simple controller can easily create the appropriate model class based on your type parameter (delegate this to a factory method, metaprogram it, or just use a case statement if that's your style), pass in all of the parameters, and let the logic in the class manage the data appropriately. All of your shared logic can live in a base class or a shared module, depending on the requirements of your data model.

Thanks for pointing me in the right direction. I have searched everywhere, but this seems to be the right place to ask rails related questions. I have started drying up the controller as you have suggested. Now I encounter another issue. I do not have database table corresponding to these models. So, can I still use the Validations framework in the model? If so, how do I do it? I read this post http://pastie.org/247701 . Is it advisable to use this procedure? Or should I just implement a few validation methods in my model?

Cleaning up controllers is a good step, but if you still need to "send" values to different actions, you can use the flash (and store any object you like in there). e.g:

bq. flash[:param_one] = 23

and so on. In terms of a model with no backing, I haven't used the pastie you linked to, so I can't comment on whether it's reliable, but it seems to be the wrong approach. Model's are intended to be backed by a database, if you want to store values, but not have them saved to a database you should be constructing a plain ruby class to contain your logic. e.g

bq. Class Result
attr_accessor :param_one, :param_two, :param_three

def validate

return false if param_one.nil? || param_one == ''<br>
 ...<br>

end

def process

..logic..<br>

end end

Thank you. I will use a Ruby class.

7 Posts
Login to add your message