【问题标题】:Can I get a controller to show up in the url under a different name?我可以让控制器以不同的名称显示在 url 中吗?
【发布时间】:2015-02-15 04:24:46
【问题描述】:

我有一个名为“Pages”的控制器。我可以让它以不同的名称显示在 url 栏中吗?例如,当我渲染“show”模板时,它会显示在这个 url 下:localhost:3000/pages/:id。我可以让它显示为localhost:3000/people/:id 吗?我只关心“显示”网址;其他 url 没那么重要。

routes.rb

get "pages/results"
get "pages/index" => "pages#index", as: "index_page"
resources :pages do
  resources :categories
end

【问题讨论】:

    标签: ruby-on-rails ruby routing routes rails-routing


    【解决方案1】:

    添加您的路线:

    get '/people/:id', to: 'pages#show'
    

    并从资源中删除旧的显示路线:

    resources :pages, only: [:index, :new, :create, :edit, :update, :delete] do
      resources :categories
    end
    

    见:Rails guides about rounting

    【讨论】:

      【解决方案2】:

      你可以这样使用:

       get '/pages/:id' => "pages#show", path: 'people/:id'
      

      这样您就可以在浏览器中访问特定的节目页面。 希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-18
        • 1970-01-01
        • 1970-01-01
        • 2021-06-21
        • 2018-06-10
        • 1970-01-01
        • 1970-01-01
        • 2017-04-15
        相关资源
        最近更新 更多