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 issue
6 Posts
redirect_to issue

Im stucked with a situation where i want to redirect my request from create action to update actions directly in RESTful manner. I searched and so far im not been able to find a method to specify HTTP method (here it is PUT) with redirect_to method. Any ideas? I tried render_component also and it doesn't support REST either.

Consider how redirects work for a moment. The server responds with a redirect response code and a URL. The browser then makes another, separate, request for the specified URL. This second request is always a GET request; I believe the HTTP standard specifies this. More importantly, the second request will not contain any of the parameters from the first request, so if you were able to redirect from #create to #update you'd end up in the #update action with no data.

I suspect that redirection isn't what you're looking for. What sort of behavior are you trying to model?

I agree with Adam, you should tell us what you need your code to do, or why you need to do such redirect so we can be more helpful and give you ideas. Redirecting create to update seems forced to me, I think you should find a more natural "rails like" way to do it.

Ya i got it completely wrong in the case of specifying http method with redirect_to. Since this is just another browser request it always comes as GET. Now i come to the problem, I have created a certain resource and i use create action to create new records . also UPDATE action for update records. After sometime i noticed that there are some cases where i want to do the exactly same updation as soon as i did the creation for the particular record. That is why instead of repeating the same code in create action or taking both code out and make it common method for update and create, I tried to use update action directly. It seems not ok i guess. So i will go for the option of making it common..........

Usually, when people are wanting to use controller actions in other controller actions it's because they've got their business logic in their controller instead of in their model where it is meant to be. Really, all that should be in your controller is a call to .new or .find and to .save or .update_attributes

Take Jason's advice, the model must be fat and the controller thin, the thiner the better. Also try using callbacks and validations to get the behaviour you want.

6 Posts
Login to add your message