Rails 4 Routing from within a Nested route to view a page on another nested route

Question!

This has been resolved i will post my answer shortly

Below I have posted my routes.rb and all models/relationships that are associated with them.

What I am trying to do is post the crud links to the PatrolReports (nested to PatrolHits) on the site show page that is nested within clients.. I keep getting this error:

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]

when I use this link on the clients/1/site/1 Show Page

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

Here are all my site routes:

  # 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

Here are my Models and Relationships:

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

here is my Rake Routes Output:

                       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

Please let me know if you require any further details ill be more than happy to post it and or give access to my repo



Answers

patrol_hit_patrol_report_path(@patrol_hit, patrol_report)

Looks like patrol_report is nil.

By : Eric


Try this

CASE 
  WHEN dbo.tbl.Price IS NULL THEN CAST(dbo.tbl.cost AS NUMERIC(18 , 1)) * 2.2 
  WHEN dbo.tbl.Price = 0 THEN CAST(dbo.tbl.cost AS NUMERIC(18 , 1)) * 2.2 
  ELSE dbo.tbl.Price 
END
By : ash


You need a logical condition in the while. Perhaps

program.toUpperCase().equals("Y")
By : nicomp


This video can help you solving your question :)
By: admin