每次`params`包含`redirect_uri`值时,如何将用户重定向到自定义网页?

我正在运行Ruby on Rails 4.1,我希望实现一个function,当用户通过提供redirect_uri参数执行操作时,将用户重定向到自定义网页。 换句话说,我希望每次在params出现redirect_uri值时“触发”重定向,并且重定向应该在任何情况下发生(例如,当使用HTML或JS格式执行GET,POST,PUT和DELETE HTTP请求时) )在完成流程流程之后(例如,当HTTP动词为POST时,重定向应在提交后发生)。

奖励 – 如果可能,我想在引荐来源url来自应用程序域之外的情况下validationURL(例如,链接正确性)。

我应该如何正确地做到这一点?

在此讨论中 ,一旦您的操作遇到redirect_to/render语句,请求流将完成,并且after_action重定向可能不会被触发。

如何创建像custom_redirect_to这样的控制器操作:

 class ApplicationController < ActionController::Base def custom_redirect_to(url) redirect_to (params[:redirect_uri].present? ? params[:redirect_uri] : url) end end 

然后在控制器操作中使用此方法而不是redirect_to

 def create @post = Post.new(params) if @post.save custom_redirect_to @post else render 'new' end end