如何在进行关注时删除 :id 部分

How to remove the :id Part when doing a Concern

我的路线文件包含:

  concern :generic_table do
    get 'search_suggestions' => 'search_suggestions'
  end

  get 'human_resources/' => 'human_resources#index'
  namespace :human_resources do
    get 'settings/' => 'settings#index'
    namespace :settings do
      resources :constants do
        concerns :generic_table
      end
    end
  end

它产生:

/human_resources/settings/constants/:constant_id/search_suggestions(.:format) 

human_resources/settings/search_suggestions#search_suggestions

我正在尝试删除 /:constant_id/ 部分,它指向控制器操作:

human_resources/settings/constants/search_suggestions#search_suggestions

所以最终会是

/human_resources/settings/constants/search_suggestions(.:format)

human_resources/settings/constants/search_suggestions#search_suggestions

如何删除 /:constant_id/ 部分;并将其直接指向我的控制器操作,以便我的搜索栏访问 AJAX?

的搜索建议

我认为您只需将 resources 块替换为 namespace 块即可。生成参数的是 resources 块。

如果您需要 id 而不是 constant_id,另请参阅 member 块。

额外的一点:这可能不会生成你想要的路由助手。还有额外的选项可以传递给 namespace 并与定制助手相关。