【发布时间】:2019-07-30 12:37:37
【问题描述】:
我正在尝试允许一个资源 Site 有条件地拥有一个父资源 Organization。
这是我目前拥有的:
resources :site do
resources :posts do
resources :comments
end
end
这会导致类似的路径
/sites/1
/sites/1/edit
/sites/1/posts
/sites/1/posts/123/comments
# etc. etc.
我希望能够拥有类似的路径
/parent_id/sites/1
/parent_id/sites/1/edit
/parent_id/sites/1/posts
/parent_id/sites/1/posts/123/comments
但仅当站点属于某个组织时。
我也不想更改我网站上已经使用的每一个路径助手(实际上有数百个地方)。
这可能吗?
这是我尝试过的:
scope "(:organization_id)/", defaults: { organization_id: nil } do
resources :site
end
# ...
# in application_controller.rb
def default_url_options(options = {})
options.merge(organization_id: @site.organization_id) if @site&.organization_id
end
但这没有用。 organization_id 没有设置。
# in my views
link_to "My Site", site_path(site)
# results in /sites/1 instead of /321/sites/1
我还尝试在路由约束中设置 organization_id,但效果不佳。
【问题讨论】:
标签: ruby-on-rails rails-routing