-1

When I visit:

URL/salvage/25605

ActiveRecord shows an error RecordNotFound

Couldn't find Salvage without an ID

Parameters: {"id"=>"25605"}

Why is ActiveRecord searching for "id" when I included "typeID" in the params?

Controller

class SalvageController < ApplicationController
  def show
    @salvage = Salvage.find(params[:typeID])
  end

Model

class Salvage < ApplicationRecord
  validates :typeID, presence: true
  validates :amount, presence: true
end

View

<%= @salvage.typeID %>, <%= @salvage.amount %>

Resources

resources :salvage

Rails Routes

Prefix Verb   URI Pattern                                                                                       Controller#Action
                             salvage_new GET    /salvage/new(.:format)                                                                            salvage#new
                                    root GET    /                                                                                                 application#hello
                production_pages_copying GET    /production_pages/copying(.:format)                                                               production_pages#copying
                           salvage_index GET    /salvage(.:format)                                                                                salvage#index
                                         POST   /salvage(.:format)                                                                                salvage#create
                             new_salvage GET    /salvage/new(.:format)                                                                            salvage#new
                            edit_salvage GET    /salvage/:id/edit(.:format)                                                                       salvage#edit
                                 salvage GET    /salvage/:id(.:format)                                                                            salvage#show
                                         PATCH  /salvage/:id(.:format)                                                                            salvage#update
                                         PUT    /salvage/:id(.:format)                                                                            salvage#update
                                         DELETE /salvage/:id(.:format)                                                                            salvage#destroy

I am following the ruby on rails 6th edition tutorial on learnenough.com a second time around using my own data

Samji3877
  • 1
  • 3

1 Answers1

0

Thanks to Deepak Mahakale I ran the rails routes command and noticed :id(format) was added at the end of the GET route.

I searched stack overflow for questions regarding changing :id(format) to something else and came across:

Changing the id parameter in Rails routing

I thus changed my Resources in the router to:

resources :salvage, param: :typeID

The show view now works albeit rather slow I shall do some more research on the topic of resources to gain a clearer understanding.

I hope this answer proves useful to anyone who may stumble upon the same question

Samji3877
  • 1
  • 3