在 Rails 中删除部分 URL

问题描述 投票:0回答:1

我有一个设置,将 /upload/* 的所有请求路由到 Rails。以博客为例,我希望 Rails 将

somedomain.com/upload/article/new
视为
somedomain.com/article/new
。我最终得到以下结果。

路线.rb

Rails.application.routes.draw do
  root "articles#index"
  scope 'upload' do
    resources :articles do
      resources :comments
    end
  end
  resources :articles do
    resources :comments
  end
end

nginx.conf

location /upload/ {
            proxy_pass http://localhost:3000/;
            proxy_set_header Host $http_host;
            proxy_set_header Upgrade $http_upgrade;
    }

它可以工作,但是您可以清楚地看到相同的代码紧挨着写了两次。 我想知道是否有更合适的方法来做到这一点。

ruby-on-rails nginx nginx-reverse-proxy ruby-on-rails-7
1个回答
0
投票

Rails.application.routes.draw 做 根“文章#index”

资源:文章做 资源:评论 结束

范围“上传”,控制器:“文章”,as:“上传” 资源:文章,路径:'' do 资源:评论 结尾 结尾 结束

我删除了重复的资源:文章做...结束块在“上传”范围之外。

在“上传”范围内,我使用控制器选项来指定请求应由“文章”控制器处理。

我使用 as: 'upload' 选项为此范围分配一个名称,使路径更具可读性。

© www.soinside.com 2019 - 2024. All rights reserved.