ruby-on-railsrubyrestnamingactionpack

Which RESTful action should I use to redirect to another site?


I have an app where I try to adhere to REST.

The app receives requests for external links that don't belong to the app, so the sole purpose of the action is to redirect the request to the external URL.

My suggestion is to have the following controller/action: redirects_controller#create.

Is my thinking correct or should it be the show action instead?


Solution

  • REST (apart from Rails) is about using the correct HTTP method for the correct action. The Rails part is just using the conventional controller action for a given HTTP method.

    So, if you're doing a 301 or 302 redirect to another page, which browsers handle by issuing a GET request to the URL in the redirect response's Location header, do it in a show action. This will allow the user's browser to cache the other page when appropriate, and to not notify the user before redirecting.

    (There is a way to redirect POSTs, but you didn't mention it so I expect you're talking about regular 301/302 redirects.)