如何更改 ActiveAdmin 路由 ID 的约束?

How can I change the constraints on an ActiveAdmin route id?

我正在对使用 ActiveAdmin 的 Rails 站点进行更改,包括将 URL 中使用的标识符更改为有意义的字符串。

在 ActiveAdmin 之外,更改每个模型的 to_param 方法就足够了。这也更改了为 ActiveAdmin 页面上的链接生成的 URL,但要让 ActiveAdmin 模型响应新的 URL 还需要更改 ActiveAdmin 注册,如 this answer.

中所述。

本网站上的一个模型在一些有意义的标识符中使用了 dots/periods。 (这不是我可以改变的。)Rails' 基于扩展名的格式识别会导致这些记录出现问题。在 ActiveAdmin 之外,这些问题可以通过更改路线上的 id 约束来解决,如 this answer. (Allowing format-specifying extensions on top of this requires some extra work in the controller.) Unfortunately, changing the constraints on non-ActiveAdmin routes has no effect on any ActiveAdmin routes. It is possible to manually specify ActiveAdmin routes (rather than relying solely on ActiveAdmin.routes(self)), as described in this answer 中所述,但我还没有找到一种方法来实现与非 ActiveAdmin 相同的约束更改路线。

如何更改 ActiveAdmin 路由中的 ID 限制以允许包含点?

这里有两次获得正确路由但不允许点的尝试:

get "/admin/motors/:id", id: /[^\/]+/, controller: "admin/motors", action: "show"
get "/admin/motors/:id", constraints: { :id => /[^\/]+/ }, controller: "admin/motors", action: "show"

题目中的尝试都是正确的,但是它们必须出现在ActiveAdmin.routes(self)之前;来自 Rails Routing from the Outside In:

Rails routes are matched in the order they are specified, so if you have a resources :photos above a get 'photos/poll' the show action's route for the resources line will be matched before the get line. To fix this, move the get line above the resources line so that it is matched first.