本文介绍了如何更改对ActiveAdmin路由ID的约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm making changes to a Rails site which uses ActiveAdmin, including changing the identifiers used in URLs to meaningful strings.

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

Outside of ActiveAdmin, it was enough to change the to_param method of each model. That also changed the URLs generated for links on ActiveAdmin pages, but to get the ActiveAdmin models to respond to the new URLs also required changing the ActiveAdmin registrations as described in this answer.

此站点上的一个模型在一些有意义的标识符中使用点/句点。 (这是我不能改变的事情。)Rails基于扩展名的格式识别会导致这些记录出现问题。在ActiveAdmin之外,可以按照中所述更改路由的id约束来解决这些问题。 (在此之上允许格式指定的扩展名需要在控制器中做一些额外的工作。)不幸的是,更改非ActiveAdmin路由的约束对任何ActiveAdmin路由都没有影响。可以手动指定ActiveAdmin路由(而不是仅依赖 ActiveAdmin.routes(self)),如,但是我还没有找到一种方法来允许对非ActiveAdmin路由进行相同的约束更改。

One of the models on this site uses dots/periods in some of the meaningful identifiers. (This is not something I can change.) Rails' recognition of formats based on extensions causes problems for these records. Outside of ActiveAdmin, those problems can be resolved by changing the id constraints on the routes as described in 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, but I have not found a way to do so that allows the same change in constraints that works for the non-ActiveAdmin routes.

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

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

Here are two attempts that get the routing right but fail to allow dots:

get "/admin/motors/:id", id: /[^\/]+/, controller: "admin/motors", action: "show"



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


推荐答案

问题中的尝试都是正确的,但是,它们必须出现在 ActiveAdmin.routes(self)之前。来自:

The attempts in the question are both correct, however, they must appear BEFORE ActiveAdmin.routes(self); from Rails Routing from the Outside In:

按指定的顺序匹配路线,因此,如果在 get上方有 resources:photos 资源行中,'photos / poll' show 操作的路线将在之前匹配 get 行。要解决此问题,请将 get 行移到上方,将 resources 行匹配首先。

这篇关于如何更改对ActiveAdmin路由ID的约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 17:23