Ruby-on-rails 创建到外部 URL 的 rails 路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3622706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:08:22  来源:igfitidea点击:

Creating a rails route to an external URL

ruby-on-railsrouting

提问by Chris

A lot of my users keep going to http://(rails app URL)/blog, but I don't actually have a blog. I finally setup a Posterous blog and now want to direct my users there. Is there a way to configure this using routes.rb? Is there a better way that doesn't involve editing the httpd.conf file?

我的很多用户继续访问http://(rails app URL)/blog,但我实际上没有博客。我终于建立了一个Posterous 博客,现在想将我的用户引导到那里。有没有办法使用 routes.rb 配置它?有没有不涉及编辑 httpd.conf 文件的更好方法?

回答by nil

I know this is old, so in case someone else needs this for rails 4:

我知道这是旧的,所以万一其他人需要这个用于 rails 4:

get "/blog" => redirect("http://example.com/blog")

Use get instead of Match in Rails 4, otherwise you'll get a Runtime error

在 Rails 4 中使用 get 而不是 Match,否则你会得到一个运行时错误

回答by Marcel Hymanwerth

Depends on the Rails version you are using.

取决于您使用的 Rails 版本。

Rails 3

导轨 3

# in routes.rb
match "/blog" => redirect("http://example.com/blog"), :as => :blog

Rails 2

导轨 2

# in routes.rb
map.blog '/blog',
  :controller => "a_helper_controller",
  :action => "redirect_to_blog"

# in a_helper_controller.rb
def redirect_to_blog
  redirect_to "http://example.com/blog"
end

回答by vitkoz

For Rails 5:

对于 Rails 5:

get '/stories', to: redirect('/articles')
get '/stories', to: redirect('http://google.com')

Rails Guide source page

Rails 指南源页面

回答by Cyril Duchon-Doris

Emulate frontend routes as regular Rails controller routes

将前端路由模拟为常规 Rails 控制器路由

Background : at the beginning there was Rails monolith rendering html view. Then came a frontend React app, and the need to convert the backend to a JSON api, and generate URLs (mostly in emails) pointing to a frontend app that behaves almost exactly like a Rails controller.

背景:一开始有 Rails 整体渲染 html 视图。然后是前端 React 应用程序,需要将后端转换为 JSON api,并生成指向前端应用程序的 URL(主要在电子邮件中),该应用程序的行为几乎与 Rails 控制器完全一样。

I was looking for a way to mimick the rails resource way to construct URL but to point instead to a frontend URL and generate appropriate path_helpers, that could be used easily with my app, RSpec, cucumber/Capybara, and everything else. I found this hack around routes to emulate frontend routes, which also requires an extra param to be passed to completely desambiguate frontend VS backend routes (useful in capybara testing)

我一直在寻找一种方法来模仿 rails 资源方式来构建 URL,而是指向前端 URL 并生成适当的 path_helpers,它可以很容易地与我的应用程序、RSpec、cucumber/Capybara 和其他所有内容一起使用。我发现这个 hack 围绕路由模拟前端路由,这还需要传递一个额外的参数来完全消除前端 VS 后端路由的歧义(在水豚测试中很有用)

frontend_scope = {
  as: :frontend,
  host: Rails.configuration.frontend_host, # like https://www.example.com
  port: Rails.configuration.frontend_port, # 443
  constraints: (lambda { |request| request.params[:app] == :frontend })
}
scope(frontend_scope) do
  root to: 'static_pages_controller#home'
  resources :articles, only: [:show, :index]
end

Then in my Ruby code I can use

然后在我的 Ruby 代码中我可以使用

frontend_article_url(@article, app: :frontend)
frontend_articles_url(app: :frontend)
... (and everything that can be generated in a classic rails app)

The inconvenient is that there will be a appparameter in your redirection, that you would have to ignore on your frontend app + all other web apps like Google Analytics, Search engine, etc*.

不便之处在于app您的重定向中会有一个参数,您必须在前端应用程序 + 所有其他网络应用程序(如 Google Analytics、搜索引擎等)上忽略该参数*。

* the problem being, if you have both a /articleson your frontend and a /articleson your backend, your app will not be able to make the difference with both routes that resolve to the same URL without the help from an extra param

* 问题是,如果您/articles的前端和/articles后端都有 a ,则您的应用程序将无法在没有额外参数帮助的情况下解析为同一 URL 的两条路由产生差异