Search code examples
ruby-on-railsruby-on-rails-3.2rails-routingcontrollers

Why to rails redirects work when redirecting to a route and do not work when redirecting to an action?


I have been struggling with rails double render errors for a few weeks now (Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at mos ....etc...).

I figured out that if i have a statement like this:

redirect_to action_name and return

I get the double render error. However, when i specify a route called action_name_path, and do a statement like this:

redirect_to action_name_path

I don't get the double render error anymore. Why is that?


Solution

  • The redirect_to should be use to go to another controller or to a external web page and automatically returns something...

    When use the redirect_to you need to specify the controller and action that you want to render. You can do that by calling the route (action_name_path(:params[])).

    When you want to call other method in the same controller or render a different view you don't need to render the controller, instead of redirect_to you can use render method.

    Please see the documentation