为生成的链接添加前缀,但不为传入路由添加前缀 [英] Add a prefix to generated links, but not to incoming routes

查看:314
本文介绍了为生成的链接添加前缀,但不为传入路由添加前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的Rails 4应用程序需要通过一个古老的门户进行访问。该门户网站的工作方式是(从浏览器的角度)为每个URL添加一个前缀。

Our Rails 4 application needs to be accessible over an archaic portal. This portal works by adding (from the perspective of the browser) a prefix to each URL; this prefix is removed by the portal before forwarding the request to my application.

因此,浏览器会调用 https://portal.company.com/。 portal / prefix / xyzzy / myapp / mymodel / new ;门户网站执行其操作并请求 https://myserver.company.com/myapp/mymodel/new (以某种不相关的方式传递剥离的前缀)。前缀是动态的,可以在请求之间更改。

So the browser calls https://portal.company.com/portal/prefix/xyzzy/myapp/mymodel/new; the portal does its thing and requests https://myserver.company.com/myapp/mymodel/new (passing along the stripped prefix in some irrelevant way). The prefix is dynamic and can change between requests.

问题是门户无法重写我的应用程序提供的HTML页面。也就是说,它不放入前缀。它希望应用程序只发出相对URL,或者自己添加门户网站前缀。

The problem is that the portal is not able to rewrite the HTML pages served by my application. That is, it does not put in the prefix. It expects applications to either only emit relative URLs, or to add the portal prefix themselves.

因此:


  • 例如,常规URL / myapp / mymodel / new 必须保持直接访问应用程序时的状态(某些用户

  • 通过门户访问时,我们的应用程序必须照常理解 / myapp / mymodel / new ,但是在使用 mymodel_new_path link_to @mymodel form_for @my_model 或任何其他魔术URL生成器,都必须添加门户网站前缀。因此,应用程序发出的任何URL都必须类似于 / portal / prefix / xyzzy / myapp / mymodel / new ,其中每个请求字符串 / portal / prefix / xyzzy 由我们定义的某种方法给定( xyzzy 部分可以在请求之间进行更改。

  • A regular URL /myapp/mymodel/new, for example, must stay as is for when the application is accessed directly (for certain users which do not use the portal).
  • When accessed over the portal, our application must still understand /myapp/mymodel/new as usual, but when using mymodel_new_path or link_to @mymodel or form_for @my_model or whatever other magic URL generators there are, it has to add the portal prefix. So, any URL emitted by the application must look like /portal/prefix/xyzzy/myapp/mymodel/new where the per-request string /portal/prefix/xyzzy is given by some method defined by us (and the part xyzzy can change between requests).

如何实现?我的 routes.rb 今天看起来像这样:

How can I achieve that? My routes.rb looks like this today:

MyApp::application.routes.draw do
  scope ' /myapp' do
    get ...

这可能必须保持原样,因为当来自门户网站时,传入请求中的URL不会更改。但是我如何影响传出的URL?

This probably has to stay as is, because URLs in incoming requests do not change when coming from the portal. But how do I influence the outgoing URLs?

推荐答案

此建议将使您能够轻松地为Rails路径助手生成的URL加上前缀。根据您的要求。请注意,但是,这也会使这些扩展路径对您的应用程序有效—它们应该仅在预期的位置进行路由,但是您会在 params 哈希中获得一些额外的值您可以忽略,所以我怀疑这是可以接受的。

This suggestion will allow you to easily prefix the urls produced by the Rails path helpers as your require. Do note, however, it will also make these extended paths valid requests for your application - they shoud just route where expected but you'll get get some extra values in the params hash that you can ignore, so I suspect this is possibly acceptable.

首先,将所有前缀位添加为可选参数您的路线的基本范围:

First, add all the prefix bits as optional parameters to your routes' base scope:

scope '(:portal/)(:prefixA/)(:prefixB)/myapp' do
  # routes
end

请注意,这些可选参数不能包含 / char,而不会被路径帮助程序转义,因此,如果前缀中有几个级别(在问题中似乎已经出现),则需要一些不同的设置参数,最后一个参数后跟一个斜杠,如上所述。

Note that the those optional params cannot include the / char without it being escaped by the path helpers, so if you have a few levels in the prefix (which it appears you do in the question) you'll need a few different params, all but the last followed by a slash, as above.

完成后,您应该在 ApplicationController default_url_options c>,它将返回您在路由中所需值的哈希值:

With that done, you should define default_url_options in your ApplicationController, it should return a hash of the values you need in your routes:

def default_url_options(_options={})
  {
    portal:  'portal',
    prefixA: 'whatevertheprefixis',
    prefixB: 'nextbitoftheprefix'
  }
end

这应该做,路径助手(以及 link_to @object 等),那么每次使用它们时都应包括这些值。

And that should do it, path helpers (along with link_to @object etc) should all now include those values every time you use them.

请注意,由于开始时 portal 位也是一个可选参数,您只需在 default_url_options 中添加其他逻辑,并在不希望出现这种前缀行为的情况下让它返回空哈希。

Note that since the portal bit at the start is also an optional parameter, you can simply add additional logic to default_url_options and have it return an empty hash whenever you do not want this prefixing behaviour.

这篇关于为生成的链接添加前缀,但不为传入路由添加前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆