Rails redirect_to

Rails 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

  • String starting with the protocol: // (like http: //) - Is passed straight through as the target for redirection.

redirect_to 'http://www.rubyonrails.org'

  • String not containing a protocol - The current protocol and the host are prepended to the string.

redirect_to '/images/screenshot.jpg'

  • 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 codes because it reqires less time to understand the meaning of it. 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 the redirect_to, use return.

redirect_to article_url(@article) and return

Display a flash message upon redirect

The most elegant way to achieve this is to use 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 parameter just add it to helper method:

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

will create this path:

"/articles/1?param=foo"

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

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

will generate the same path.

Now, you can access it in 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:

  • by Will give you at The Absolute _url path, containing at The Protocol, Host and Port.
    the For Example:  http://localhost:3000/articles.

  • _path will return the relative path which is without the domain, protocol etc.
    So it would be just  /articles.

Use redirect_back instead of redirect_to: back

Rails 5 provides redirect_back to redirect to HTTP_REFERER. When HTTP_REFERER is not present, it redirects to whatever is passed as fallback_location.

redirect_back(fallback_location: root_path)

You can pass 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. A render does not change the URL of the page you are visiting.

We have come to an end

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

Originally published on kolosek.com .

转载于:https://my.oschina.net/u/3772078/blog/1619352

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值