title: Rails associations and passing another model :id as an argument
body: <p>I have two associated models: Job and Candidate. The Candidate model is updated when a user applies to a job listing through the candidates 'new' view. Since I've associated Candidate with the Job model, I can't save any new instances of Candidate. I know that I have to pass job_id as a parameter so that any new instance of Candidate references a particular Job, but I just cant figure out what I'm doing wrong. Also, the Job model is associated with the Devise User model, but Candidate is not associated with the User model. Could that be the issue? Thanks in advance.</p>
  
  <p>I've tried a ton of things in my candidates controller, but now I've put it back to how it was before I associated the Candidate model with Job model, so I can start over again as if I just ran the migration to AddJobToCandidate</p>
  
  <p>candidates_controller.rb</p>
  
  <pre><code>class CandidatesController &lt; ApplicationController
    def index
      @candidates = Candidate.all
    end
  
    def new
      @candidate = Candidate.new
      render layout: false
    end
  
    def single
      @candidate = Candidate.find(params[:id])
    end
  
    def create
      @candidate = Candidate.new(candidate_params)
      if @candidate.save
        redirect_to root_path
      else
        render "new"
      end
    end
  
    private 
    def candidate_params 
      params.require(:candidate)
            .permit(:first_name, :last_name, :email, :cover_letter, :ph, :job_id)
    end
  
  end
  </code></pre>
  
  <p>views/candidates/new.html.erb</p>
  
  <pre><code>&lt;h1&gt;Work at &lt;%= current_user.company %&gt;&lt;/h1&gt;
  
  &lt;%= simple_form_for @candidate do |form| %&gt;
    &lt;%= form.input :first_name, label: "First Name" %&gt;
    &lt;%= form.input :last_name, label: "Last Name" %&gt;
    &lt;%= form.input :email, label: "Email" %&gt;
    &lt;%= form.input :cover_letter, label: "Cover Letter" %&gt;
    &lt;%= form.input :ph, label: "Phone Number" %&gt;
  
  
    &lt;%= form.button :submit %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>routes.rb</p>
  
  <pre><code>Rails.application.routes.draw do
    get 'candidates/index'
    get 'candidates/new/:id' =&gt; 'candidates#new', :as =&gt; 'apply'
    get 'candidates/single/:id' =&gt; 'candidates#single', :as =&gt; 'viewcandidate'
    get 'jobs/index'
    get 'jobs/new'
    get 'jobs/listing/:id' =&gt; 'jobs#listing', as: 'listing'
    get 'jobs/listings', :as =&gt; 'careerpage'
    get 'jobs/dashboard'
    get 'jobs/single/:id' =&gt; 'jobs#single', :as =&gt; 'viewjob'
    devise_for :users, controllers: { sessions: "users/sessions" }
    resources :candidates
    resources :jobs
    root 'jobs#dashboard'
  end
  </code></pre>
  
  <p>views/jobs/listing.html.erb <em>the only view that links to candidates/new.html/erb</em></p>
  
  <pre><code>&lt;h1&gt;&lt;%= @job.title %&gt;&lt;/h1&gt;
  &lt;p&gt;&lt;%= @job.description %&gt;&lt;/p&gt;
  &lt;p&gt;&lt;%= link_to "Apply", apply_path(@job.id) %&gt;&lt;/p&gt;
  </code></pre>
  
  <p>Candidate/Job association migration</p>
  
  <pre><code>class AddJobToCandidate &lt; ActiveRecord::Migration[5.2]
    def change
      add_reference :candidates, :job, foreign_key: true
    end
  end
  </code></pre>
  
  <p>jobs_controller.rb (don't think this is necessary but I'll include anyway)</p>
  
  <pre><code>class JobsController &lt; ApplicationController
     before_action :authenticate_user!
  
    def index
      @jobs = current_user.jobs.all
    end
  
    def dashboard
  
    end
  
    def new
      @job = Job.new
    end
  
    def listing
      @job = Job.find(params[:id])
      render layout: false
    end
  
    def listings
      @jobs = current_user.jobs.all
      render layout: false
    end
  
    def single
      @job = Job.find(params[:id])
      @candidates = Candidate.all
    end
  
    def create
      @job = current_user.jobs.new(job_params)
      if @job.save
        redirect_to jobs_index_path
      else
        render "new"
      end
    end
  
    private 
    def job_params 
      params.require(:job)
            .permit(:title, :description, :url, :user_id)
    end
  
  end
  </code></pre>
  
  <p>No error is shown, but model is not updated. </p>
  
  <p>One of the things I tried in candidate_controller #create action was:</p>
  
  <pre><code>def create
      @candidate = @job.candidates.create(candidate_params)
      if @candidate.save
        redirect_to root_path
      else
        render "new"
      end
    end
  </code></pre>
  
  <p>and the error I get is 'undefined method `candidates' for nil:NilClass'</p>
  
  <p>Another thing I tried was adding this line in the #create action:</p>
  
  <pre><code>@job = Job.find(params[:id])
  </code></pre>
  
  <p>and the error I get is 'Couldn't find Job without an ID'</p>