如何在 Rails 路径中声明范围?

How to declare scope in Rails paths?

我有一个文章资源,它被定义为平面资源,但也定义为可变用户的范围:

resources :articles
scope ':user' do
  resources :articles
end

在这种情况下,article_path 123 将生成 /articles/123。如何生成作用域路径,例如/mary/articles/123?例如,article_path 123, user: 'mary' 不起作用;它只是添加 ?user=mary.

rails routes 表示作用域路径没有特殊名称:

article_path    GET /articles/:id(.:format) articles#show
                ...
                GET /:user/articles(.:format) articles#index

尝试添加,as 来创建路由助手

scope ':user', as: 'user' do
  resources :articles
end

你会得到

user_articles_path(user, article)

也检查routing