【问题标题】:Rails route alias for a nested route with specific id具有特定 ID 的嵌套路由的 Rails 路由别名
【发布时间】:2020-09-07 14:15:07
【问题描述】:

我正在尝试为我的 Rails 路线创建一些别名,例如 'events/8/event_participants/new'/business-meet/registration, 我的路线是这样写的:

  resources :events, only: [], shallow: true do
    resources :event_participants, only: [:new, :create, :edit, :update] do
      post :complete, on: :collection

      member do
        get :invite, :add_people, :accept_invitation, :invitation_success, :reserved
        put :refer
      end
    end
  end

我想要特定事件 id 8 的别名,我尝试使用 redirect,但它实际上重定向到路由,因此整个路由在浏览器中可见,我希望保持可见 /business-meet/registration 路由到我的浏览器。

【问题讨论】:

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


    【解决方案1】:

    您可以在路由中使用match 方法。

    match '/business-meet/registration', to: 'event_participants#new', via: :get, defaults: { event_id: 8 }
    

    【讨论】:

    • 我没有任何事件控制器,因为我不必在事件中执行任何操作,它会给出此错误Routing Error uninitialized constant Events,如果我将事件控制器设为空,则事件将不起作用
    • 那你是怎么知道事件的呢?您可以从路线中删除事件。我已经更新了答案。
    • 您可以在我的问题中看到我的路线是如何为该部分定义的
    • 我仍然看不到您的控制器以及与哪个参数 8 匹配。更新没有用吗?我认为您需要对其进行调整以匹配您的代码,但这就是您如何做到这一点的答案。
    猜你喜欢
    • 1970-01-01
    • 2019-10-18
    • 2013-09-23
    • 1970-01-01
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    相关资源
    最近更新 更多