【问题标题】:Rails 4 Routing from within a Nested route to view a page on another nested routeRails 4从嵌套路由中路由以查看另一个嵌套路由上的页面
【发布时间】:2016-09-28 22:24:01
【问题描述】:

问题已解决,我会尽快发布答案

我在下面发布了我的 routes.rb 以及与之关联的所有模型/关系。

我要做的是在嵌套在客户端中的站点显示页面上发布指向 PatrolReports(嵌套到 PatrolHits)的 crud 链接。我不断收到此错误:

ActionController::UrlGenerationError at /clients/1/sites/2
No route matches {:action=>"show", :controller=>"patrol_hits/patrol_reports", :id=>"1", :patrol_hit_id=>nil} missing required keys: [:patrol_hit_id]

当我在 clients/1/site/1 Show Page 上使用此链接时

<td><center><%= link_to "View", patrol_hit_patrol_report_path(@patrol_hit, patrol_report) %></center></td>

这是我所有的站点路由:

  # Nests Sites To Clients
  resources :clients, controller: 'clients' do
    resources :sites, controller: 'clients/sites', except: [:index]
  end

  # Nests PatrolHit To PatrolRoutes
  resources :patrol_routes, controller: 'patrol_routes' do
    resources :patrol_hits, controller: 'patrol_routes/patrol_hits', except: [:index]
  end

  # Nests PatrolReport to PatrolHits
  resources :patrol_hits, controller: 'patrol_routes/patrol_hits', except: [:index] do
    resources :patrol_reports, controller: 'patrol_hits/patrol_reports', except: [:index]
  end

这是我的模型和关系:

class Client < ApplicationRecord
  has_many :sites, dependent: :destroy
end

class Site < ApplicationRecord
  belongs_to :client
  has_many :patrol_hits
  has_many :patrol_reports, dependent: :destroy
end

class PatrolRoute < ApplicationRecord
  has_many :patrol_hits, dependent: :destroy
end

class PatrolHit < ApplicationRecord
  belongs_to :site
  belongs_to :patrol_route
  has_many :patrol_reports, dependent: :destroy
end

class PatrolReport < ApplicationRecord
  belongs_to :patrol_hit
  belongs_to :site
  belongs_to :user
end

这是我的 Rake Routes 输出:

                       Prefix Verb   URI Pattern                                                    Controller#Action

                 client_sites POST   /clients/:client_id/sites(.:format)                            clients/sites#create
              new_client_site GET    /clients/:client_id/sites/new(.:format)                        clients/sites#new
             edit_client_site GET    /clients/:client_id/sites/:id/edit(.:format)                   clients/sites#edit
                  client_site GET    /clients/:client_id/sites/:id(.:format)                        clients/sites#show
                              PATCH  /clients/:client_id/sites/:id(.:format)                        clients/sites#update
                              PUT    /clients/:client_id/sites/:id(.:format)                        clients/sites#update
                              DELETE /clients/:client_id/sites/:id(.:format)                        clients/sites#destroy
                      clients GET    /clients(.:format)                                             clients#index
                              POST   /clients(.:format)                                             clients#create
                   new_client GET    /clients/new(.:format)                                         clients#new
                  edit_client GET    /clients/:id/edit(.:format)                                    clients#edit
                       client GET    /clients/:id(.:format)                                         clients#show
                              PATCH  /clients/:id(.:format)                                         clients#update
                              PUT    /clients/:id(.:format)                                         clients#update
                              DELETE /clients/:id(.:format)                                         clients#destroy
     patrol_route_patrol_hits POST   /patrol_routes/:patrol_route_id/patrol_hits(.:format)          patrol_routes/patrol_hits#create
  new_patrol_route_patrol_hit GET    /patrol_routes/:patrol_route_id/patrol_hits/new(.:format)      patrol_routes/patrol_hits#new
 edit_patrol_route_patrol_hit GET    /patrol_routes/:patrol_route_id/patrol_hits/:id/edit(.:format) patrol_routes/patrol_hits#edit
      patrol_route_patrol_hit GET    /patrol_routes/:patrol_route_id/patrol_hits/:id(.:format)      patrol_routes/patrol_hits#show
                              PATCH  /patrol_routes/:patrol_route_id/patrol_hits/:id(.:format)      patrol_routes/patrol_hits#update
                              PUT    /patrol_routes/:patrol_route_id/patrol_hits/:id(.:format)      patrol_routes/patrol_hits#update
                              DELETE /patrol_routes/:patrol_route_id/patrol_hits/:id(.:format)      patrol_routes/patrol_hits#destroy
                patrol_routes GET    /patrol_routes(.:format)                                       patrol_routes#index
                              POST   /patrol_routes(.:format)                                       patrol_routes#create
             new_patrol_route GET    /patrol_routes/new(.:format)                                   patrol_routes#new
            edit_patrol_route GET    /patrol_routes/:id/edit(.:format)                              patrol_routes#edit
                 patrol_route GET    /patrol_routes/:id(.:format)                                   patrol_routes#show
                              PATCH  /patrol_routes/:id(.:format)                                   patrol_routes#update
                              PUT    /patrol_routes/:id(.:format)                                   patrol_routes#update
                              DELETE /patrol_routes/:id(.:format)                                   patrol_routes#destroy
    patrol_hit_patrol_reports POST   /patrol_hits/:patrol_hit_id/patrol_reports(.:format)           patrol_hits/patrol_reports#create
 new_patrol_hit_patrol_report GET    /patrol_hits/:patrol_hit_id/patrol_reports/new(.:format)       patrol_hits/patrol_reports#new
edit_patrol_hit_patrol_report GET    /patrol_hits/:patrol_hit_id/patrol_reports/:id/edit(.:format)  patrol_hits/patrol_reports#edit
     patrol_hit_patrol_report GET    /patrol_hits/:patrol_hit_id/patrol_reports/:id(.:format)       patrol_hits/patrol_reports#show
                              PATCH  /patrol_hits/:patrol_hit_id/patrol_reports/:id(.:format)       patrol_hits/patrol_reports#update
                              PUT    /patrol_hits/:patrol_hit_id/patrol_reports/:id(.:format)       patrol_hits/patrol_reports#update
                              DELETE /patrol_hits/:patrol_hit_id/patrol_reports/:id(.:format)       patrol_hits/patrol_reports#destroy
                  patrol_hits POST   /patrol_hits(.:format)                                         patrol_routes/patrol_hits#create
               new_patrol_hit GET    /patrol_hits/new(.:format)                                     patrol_routes/patrol_hits#new
              edit_patrol_hit GET    /patrol_hits/:id/edit(.:format)                                patrol_routes/patrol_hits#edit
                   patrol_hit GET    /patrol_hits/:id(.:format)                                     patrol_routes/patrol_hits#show
                              PATCH  /patrol_hits/:id(.:format)                                     patrol_routes/patrol_hits#update
                              PUT    /patrol_hits/:id(.:format)                                     patrol_routes/patrol_hits#update
                              DELETE /patrol_hits/:id(.:format)                                     patrol_routes/patrol_hits#destroy
                  static_home GET    /static/home(.:format)                                         static#home
                 static_about GET    /static/about(.:format)                                        static#about
                  static_help GET    /static/help(.:format)                                         static#help
       static_feature_request GET    /static/feature_request(.:format)                              static#feature_request

如果您需要任何进一步的详细信息,请告诉我,我们非常乐意发布或访问我的仓库

【问题讨论】:

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


    【解决方案1】:

    patrol_hit_patrol_report_path(@patrol_hit, patrol_report)

    看起来patrol_report 为零。

    【讨论】:

    • 我实际上刚刚解决了这个问题,我会在几个之后发布。谢谢你
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 2023-01-02
    • 2015-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多