All threads / ActiveRecord/Routing madness

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

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).

Join the discussion

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

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

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

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.