ActiveRecord::RecordNotFound in EmployeesController#edit

I am getting an error when trying to edit an employee. The full error message is:

ActiveRecord::RecordNotFound in EmployeesController#edit

Couldn’t find Company with ‘id’=5 (*this varies depending on which company I click on)

Extracted source (around line #57):

def find_company
    @company = Company.find(permitted_params[:company_id])
  end
end

With this application I have companies and employees. I can add, show and delete employees within the companies but I am unable to edit them due to this error. I am new to Rails and am unsure where I have gone wrong.

routes.rb:

Rails.application.routes.draw do
  get 'welcome/index'

  resources :companies do
    resources :employees
  end

  root 'welcome#index'

  get 'companies/new'
end

Employees controller:

class EmployeesController < ApplicationController
  before_action :find_company
  before_action :set_employee, only: %i[show edit update destroy]

  def index
    @employees = @company.employees
  end

  def show
    @employee = Employee.find(params[:id])
  end

  def new
    @employee = @company.employees.build
  end

  def edit
    @employee = Employee.find(params[:id])
  end

  def create
    @employee = @company.employees.build(permitted_params[:employee])
    if @employee.save
      redirect_to company_employees_path(@company)
    else
      render 'new'
    end
  end

  def update
    if @employee.update(forename: permitted_params[:forename],
                        surname: permitted_params[:surname])
      redirect_to company_employees_path(@company)
    else
      render 'edit'
    end
  end

  def destroy
    @employee.destroy
    redirect_to company_employees_path(@company)
  end

  private

  def set_employee
    @employee = Employee.find(permitted_params[:id])
  end

  def permitted_params
    params.permit!
  end

  def find_company
    @company = Company.find(permitted_params[:company_id])
  end
end

Companies controller:

class CompaniesController < ApplicationController

  def index
    @company = Company.all
  end

  def show
    @company = Company.find(permitted_params[:id])
    # @company = Company.first
  end

  def new
    @company = Company.new
  end

  def create
    @company = Company.new(permitted_params[:company])

    if @company.save
      redirect_to @company
    else
      render 'new'
    end
  end

  def destroy
    @company = Company.find(permitted_params[:id])
    @company.destroy

    redirect_to companies_path
  end

  private

  def permitted_params
    params.permit!
  end
end

Rake routes output:

Prefix Verb   URI Pattern                                                                              Controller#Action
                        welcome_index GET    /welcome/index(.:format)                                                                 welcome#index
                    company_employees GET    /companies/:company_id/employees(.:format)                                               employees#index
                                      POST   /companies/:company_id/employees(.:format)                                               employees#create
                 new_company_employee GET    /companies/:company_id/employees/new(.:format)                                           employees#new
                edit_company_employee GET    /companies/:company_id/employees/:id/edit(.:format)                                      employees#edit
                     company_employee GET    /companies/:company_id/employees/:id(.:format)                                           employees#show
                                      PATCH  /companies/:company_id/employees/:id(.:format)                                           employees#update
                                      PUT    /companies/:company_id/employees/:id(.:format)                                           employees#update
                                      DELETE /companies/:company_id/employees/:id(.:format)                                           employees#destroy
                            companies GET    /companies(.:format)                                                                     companies#index
                                      POST   /companies(.:format)                                                                     companies#create
                          new_company GET    /companies/new(.:format)                                                                 companies#new
                         edit_company GET    /companies/:id/edit(.:format)                                                            companies#edit
                              company GET    /companies/:id(.:format)                                                                 companies#show
                                      PATCH  /companies/:id(.:format)                                                                 companies#update
                                      PUT    /companies/:id(.:format)                                                                 companies#update
                                      DELETE /companies/:id(.:format)                                                                 companies#destroy
                                 root GET    /                                                                                        welcome#index
                        companies_new GET    /companies/new(.:format)                                                                 companies#new

Company show.html.erb

<h3><%= @company.name %></h3>
<p><%= @company.details %></p>

<h3>Employees</h3>
<table class="table table-striped">
  <thead>
    <tr>
      <th>ID</th>
      <th>Forename</th>
      <th>Surname</th>
      <th>Actions</th>
    </tr>
  </thead>

  <tbody>
    <% for employee in @company.employees %>
      <tr>
        <td><%= employee.id %></td>
        <td><%= employee.forename %></td>
        <td><%= employee.surname %></td>
        <td><%= link_to "Edit", edit_company_employee_path(employee), class: "btn btn-primary btn-sm" %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<%= link_to "Add Employee", new_company_employee_path(@company), class: "btn btn-primary" %>

<%= link_to "Back to companies List", companies_path, class: "btn btn-outline-primary" %>

Please can someone help?

Answer

Try to pass @company to edit_company_employee_path.

<%= link_to "Edit", edit_company_employee_path(@company, employee), class: "btn btn-primary btn-sm" %>