In a Rails app, which statement can be used to send the user to a different page?
a. route_to
b. redirect_to
c. continue_to
d. send_to
The correct answer and explanation is:
Correct Answer: b. redirect_to
In a Ruby on Rails application, the correct method to send the user to a different page is redirect_to
. This method is used in controllers to perform an HTTP redirect, which tells the browser to make a new request to a different URL. It is a common part of the Post/Redirect/Get pattern, which is used to avoid duplicate form submissions and to improve user experience.
For example, after a user successfully submits a form to create a new resource, you might want to redirect them to the show page of that resource:
redirect_to @article
This line sends the browser to the page that displays the newly created article. Alternatively, you can redirect to a specific URL or named route:
redirect_to "/welcome"
redirect_to articles_path
The redirect_to
method works by returning a 302 Found HTTP status code by default, unless otherwise specified. This tells the client’s browser to initiate a second request to the new location.
The other options in the question are incorrect:
- route_to is not a valid Rails method. Routes are defined in the
routes.rb
file, but they are not used this way in controllers. - continue_to does not exist in Rails. It is not part of the framework’s redirection or routing system.
- send_to is also not valid in Rails for redirection. While
send
is a Ruby method used to call methods dynamically, it is unrelated to routing or HTTP behavior.
Thus, redirect_to
is the correct and widely used method to navigate users between different pages in a Rails app.