Ask A Question

Notifications

You’re not receiving notifications from this thread.

ActiveRecord/Routing madness

Nelson Casanova asked in Rails

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

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,796+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.