DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Implementing WOPI Protocol For Office Integration
  • Mule 3 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • Mastering Concurrency: An In-Depth Guide to Java's ExecutorService
  • DataWeave: Play With Dates (Part 1)

Trending

  • Manual Sharding in PostgreSQL: A Step-by-Step Implementation Guide
  • Endpoint Security Controls: Designing a Secure Endpoint Architecture, Part 2
  • AWS to Azure Migration: A Cloudy Journey of Challenges and Triumphs
  • How to Perform Custom Error Handling With ANTLR
  1. DZone
  2. Data Engineering
  3. Data
  4. Rails's redirect_to

Rails's redirect_to

In this article, we take an up-close-and-personal look at the Rails framework by getting into the nitty-gritty of this particular functionality.

By 
Nesha Zoric user avatar
Nesha Zoric
·
Feb. 09, 18 · Tutorial
Likes (34)
Comment
Save
Tweet
Share
56.6K Views

Join the DZone community and get the full member experience.

Join For Free

Rails's redirect_to takes two parameters, option and response_status  (optional). It redirects the browser to the target specified in options.

This parameter can be:

  • Hash - The URL will be generated by calling url_for with the options.
    • redirect_to action: 'show', id: 2

  • Record - The URL will be generated by calling url_for with the options.
    • redirect_to article

  • A string starting with the protocol - // (like http: //) - is passed straight through as the target for redirection.
    • redirect_to 'http://www.rubyonrails.org'

  • A string not containing a protocol, the current protocol, and the host are prepended to the string.

redirect_to '/images/screenshot.jpg'

  • A helper method generated by Rails (most commonly used).
    • redirect_to articles_url

  • :back - Back to the page that issued the request. Useful for forms that are triggered from multiple places.
    • redirect_to :back

The redirection happens as a "302 Moved" header unless otherwise specified.

redirect_to profile_url(@profile), :status => :moved_permanently
redirect_to profile_url(@profile), :status => 302

Note: You should always use named code because it is more readable. Be sure to check other status codes.

If needed, you can also specify a controller and an action.

  • redirect_to :controller => 'article', :action => 'index'

You can specify the format, too (in case you need to redirect a request coming in one format to another format).

  • redirect_to :action => 'show', :format => 'html'

Redirect to your main page, just make sure to configure to root route first.

  • redirect_to :root

Also, redirect_to does not stop the execution of the function. To terminate the execution of the function immediately after redirect_to, use return.

  • redirect_to article_url(@article) and return

Display a Flash Message On Redirect

The most elegant way to achieve this is to use a helper method in ApplicationController and declare which types of flash messages you want to use. For example:

class ApplicationController < ActionController::Base
  ...
  add_flash_types :success, :error
  ...
end

Then, use it in your controller actions like this:

  • redirect_to articles_path, success: 'Article is successfully created'

Passing Parameters in redirect_to

If you need to pass additional parameters, just add it to helper method:

  • redirect_to article_path(@article, param: 'foo')

The above code will create this path:

  • "/articles/1?param=foo"

Or, if you want to specify the exact controller and action, do the following:

  • redirect_to controller: 'articles', action: 'show', id: 1, param: 'foo'

The above will generate the same path.

Now, you can access it in the responding action and do what you intended with it:

def show
  @param = params[:param]
  ...
end

Redirect to Subdomain

If you're looking to redirect to a subdomain try this:

  • redirect_to article_url(@article, subdomain: 'sub')

Difference Between _url and _path

The main difference between _url and _path is:

  • _urlwill give you the absolute URL path, containing the protocol, host, and port. For example, http://localhost:3000/articles.
  • _path will return the relative path, which is given without the domain, protocol, etc. So it would just be /articles.

Use redirect_back Instead of redirect_to: back

Rails 5 provides a redirect_back function to redirect the user to HTTP_REFERER. When HTTP_REFERER is not present, it redirects to the user whatever is passed in as fallback_location.

  • redirect_back(fallback_location: root_path)

You can pass in a notice, too:

  • redirect_back(fallback_location: root_path, notice: "Your message")

The Difference Between redirect_to and Render

Redirect is telling the browser it needs to make a new request to a different location. Render does not change the URL of the page you are visiting.

Conclusion

Thank you for reading this article, we hope we were able to help!

References:
https://apidock.com/rails/ActionController/Base/redirect_to
http://api.rubyonrails.org/classes/ActionController/Redirecting.html
https://tosbourn.com/difference-between-redirect-render- rails /
https://blog.bigbinary.com/2016/02/29/rails-5-improves-redirect_to_back-with-redirect-back.html
https://stackoverflow.com/questions/2350539/what-is-the -difference-between-url-and-path-while-using-the-routes-in-rails

Protocol (object-oriented programming) Host (Unix) Strings Requests Data Types Execution (computing) Pass (software) Form (document)

Opinions expressed by DZone contributors are their own.

Related

  • Implementing WOPI Protocol For Office Integration
  • Mule 3 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • Mastering Concurrency: An In-Depth Guide to Java's ExecutorService
  • DataWeave: Play With Dates (Part 1)

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!