You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:Rails 5 ActiveAdmin应用路由匹配失败,提示缺少cohort_id参数

解决Rails 5 ActiveAdmin路由匹配错误:missing required keys: [:cohort_id]

这个错误的核心原因很明确:你尝试访问admin/genius#index路由,但该路由是嵌套在admin/cohorts资源下的子资源,必须传入父资源的cohort_id参数才能匹配成功。

为什么会出现这个问题?

从你提供的路由列表来看,只显示了admin/cohorts的路由,但报错指向admin/genius,说明你在ActiveAdmin中应该是把Genius注册为了Cohort的嵌套资源,类似这样的配置:

# app/admin/cohorts.rb
ActiveAdmin.register Cohort do
  # 嵌套Genius资源
  resources :genius
end

这种情况下,实际生成的路由会是嵌套结构(你可以通过rails routes | grep genius命令查看完整路由),比如:

admin_cohort_genius GET    /admin/cohorts/:cohort_id/genius(.:format)          admin/genius#index

可以看到,路由明确要求cohort_id作为必填参数,不传就会触发你看到的错误。

解决方案:传递必填的cohort_id参数

你需要在调用路由助手方法时,传入cohort实例或者cohort_id参数:

1. 在视图/页面中链接到Genius列表

如果是在Cohort的详情页或者有@cohort实例的场景:

# 正确写法1:传入cohort实例
link_to '查看所有Genius', admin_cohort_genius_index_path(@cohort)

# 正确写法2:直接指定cohort_id参数
link_to '查看所有Genius', admin_cohort_genius_index_path(cohort_id: @cohort.id)

2. 在ActiveAdmin的Cohort页面中使用

如果是在Cohort的ActiveAdmin配置里(比如show页面),可以用resource变量获取当前的Cohort实例:

# app/admin/cohorts.rb
ActiveAdmin.register Cohort do
  show do
    panel '关联的Genius' do
      link_to '查看全部', admin_cohort_genius_index_path(resource)
    end
  end
end

3. 验证路由结构

先确保你清楚完整的路由规则,执行以下命令查看genius相关的所有路由:

rails routes | grep genius

确认路由确实是嵌套在cohort下的,这样就能明确需要传递的参数。

内容的提问来源于stack exchange,提问作者Hodari Toure'

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:30:16