[英]How to get vanity_url resolved in ActiveAdmin belongs_to?
I'm adding ActiveAdmin to a mature application and having difficulty getting the route generation to work for a belongs_to relationship. 我正在将ActiveAdmin添加到成熟的应用程序中,并且难以使路由生成工作以适用于Emirates_to关系。 Here is the class relationship:
这是类关系:
class Project < ActiveRecord::Base
def to_param
vanity_url.blank? ? id.to_s : vanity_url
end
end
class Job < ActiveRecord::Base
belongs_to :project
end
This is important because we use routes such as domain/projects/awesome_project
instead of domain/projects/215
. 这很重要,因为我们使用的是诸如
domain/projects/awesome_project
路由,而不是domain/projects/215
。 All pretty standard stuff. 所有漂亮的标准东西。
In ActiveRecord, I've gotten the relation correctly defined with: 在ActiveRecord中,我已经使用以下方式正确定义了关系:
ActiveAdmin.register Project do
end
ActiveAdmin.register Job do
belongs_to :project
end
I believe this is correct because the route generated include: 我认为这是正确的,因为生成的路由包括:
admin_project_jobs GET /admin/projects/:project_id/jobs(.:format) admin/jobs#index
POST /admin/projects/:project_id/jobs(.:format) admin/jobs#create
new_admin_project_job GET /admin/projects/:project_id/jobs/new(.:format) admin/jobs#new
edit_admin_project_job GET /admin/projects/:project_id/jobs/:id/edit(.:format) admin/jobs#edit
admin_project_job GET /admin/projects/:project_id/jobs/:id(.:format) admin/jobs#show
... etc
When I use a route generator, such as admin_project_jobs_path(@project)
, the correct route is generated; 当我使用路由生成器时,例如
admin_project_jobs_path(@project)
,将生成正确的路由。 that is: /admin/projects/awesome_project/jobs
. 即:
/admin/projects/awesome_project/jobs
。
The problem is that with in the ActiveAdmin Jobs controller, the project_id
value of "awesome_project" is does not result in the correct parent object. 问题在于,在ActiveAdmin Jobs控制器中,“ awesome_project”的
project_id
值不会导致正确的父对象。 If I change the route generation to admin_project_jobs_path(@project.id)
to use the ID of the object instead of it's vanity_url, that results in a path like /admin/projects/215/jobs
, which is correctly resolved. 如果我将路由生成更改为
admin_project_jobs_path(@project.id)
以使用对象的ID而不是vanity_url,则将导致类似/admin/projects/215/jobs
的路径,该路径可以正确解析。 However ActiveAdmin generates other paths and those use the object, which resolve back to the vanity_url, so I can't side step this problem forever. 但是ActiveAdmin会生成其他路径, 这些路径会使用对象,这些路径会解析回vanity_url,因此我无法永远避免此问题。
I've read through the code to see where the path is generated, but it seems the real solution should be in the Project or the Job ActiveAdmin object. 我通读了代码以查看生成路径的位置,但是真正的解决方案似乎应该在Project或Job ActiveAdmin对象中。 I've tried putting custom
find_resource
blocks into both, but haven't found something that does what is should. 我尝试将自定义的
find_resource
块放到这两者中,但是还没有找到可以做的事情。
Has anyone worked with such a scenario, or understands more about how the ActiveAdmin resources are resolved? 是否有人在这种情况下工作过,或者是否了解有关如何解决ActiveAdmin资源的更多信息? Thanks.
谢谢。
ActiveAdmin.register Job do
belongs_to :project, :finder => :find_by_title! # or what can find your model
end
For more details see https://github.com/josevalim/inherited_resources#belongs-to 有关更多详细信息,请参见https://github.com/josevalim/inherited_resources#belongs-to
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.