Skip to main content

ActiveRecord/Routing madness

Rails • Asked by Nelson Casanova

I'm not sure whats happening here, I feel like I've done this successfully in the past but for some reason my app is crashing all over.

This is quite simple. Ive got a model Employees and it has a relationship with another model called Medicals. So basically each employee can have multiple medicals.

routes.rb looks like:

Rails.application.routes.draw do
  resources :employees do
    resources :medicals
  end
end

The MedicalsController.rb looks like:

class MedicalsController < ApplicationController
  def show
    @medical = Medical.find(params[:id])
  end

  def new
    @medical = Medical.new(employee_id: params[:employee_id])
  end

  def create
    @medical = Medical.new(medical_params)

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

  def edit
    @medical = Medical.find(params[:id])
  end

  def update
    @medical = Medical.find(params[:id])

    if @medical.update(medical_params)
      redirect_to @medical
    else
      render 'edit'
    end
  end

  private
  def medical_params
    params.require(:medical).permit(:employee_id, :date_signed, :date_valid, :medical_notes)
  end
end

And lastly views\medicals\new.html.erb looks like:

<%= simple_form_for @medical, url: employee_medicals_path(@medical) do |form| %>
  <%= form.input :date_signed %>
  <%= form.input :date_valid %>
  <%= form.button :submit %>
<% end %>

Im using the simple_form gem but that should be the issue, i would think...

Anyway, I'm getting the following error on

https://pasteboard.co/IjKcQTb.png

You can see in the live shell that I tried other paths to no avail... they all error out so I'm not sure whats missing here....

employee_medicals_path
=> "/employees/2/medicals"

employee_medicals_path(@medical)
!! #"index", :controller=>"medicals", :employee_id=>nil}, missing required keys: [:employee_id]>
employee_medical_path(@medical)
!! #"show", :controller=>"medicals", :employee_id=>#}, missing required keys: [:id], possible unmatched constraints: [:employee_id]>

medicals_path(@medical)
!! #:0x00007fc9e0992738>>

finally rake routes:

        employee_medicals GET    /employees/:employee_id/medicals(.:format)                                               medicals#index
                          POST   /employees/:employee_id/medicals(.:format)                                               medicals#create
     new_employee_medical GET    /employees/:employee_id/medicals/new(.:format)                                           medicals#new
    edit_employee_medical GET    /employees/:employee_id/medicals/:id/edit(.:format)                                      medicals#edit
         employee_medical GET    /employees/:employee_id/medicals/:id(.:format)                                           medicals#show
                          PATCH  /employees/:employee_id/medicals/:id(.:format)                                           medicals#update
                          PUT    /employees/:employee_id/medicals/:id(.:format)                                           medicals#update
                          DELETE /employees/:employee_id/medicals/:id(.:format)                                           medicals#destroy

Looking at the error in your browser (and at your routes); your path needs an employee_id, but it looks like you're passing a Medical object instead. You probably want to create an instant variable for the Employee, so you can reference it in your create (and new) action, eg. @employee = Employee.find(params[:employee_id]). Use that instead of @medical.

As you you use default nested routes, you should be able to use simple_form_for [@employee, @medical] do |form| instead (once you've set @employee). But not 100% certain, as it might be different for simple_form (which I don't know).


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 22,346+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.