Search code examples
ruby-on-railsroutescontrollers

Routing back by ID in Rails


I have a rails app that has associated jobs and customers. A customer has_many jobs and a job belongs_to a customer.

On my customer show page, there is a link to create a new job:

<%= link_to "Add New Job", new_customer_job_path(@customer) %>

After clicking that link they are sent to a new form page for a new job. When the form is submitted, the request gets pushed to the jobs controller as follows:

class JobsController < ApplicationController
  before_action :set_job, only: [:show, :edit, :update, :destroy]

def index
  @jobs = Job.all
end

def show
end

def new
  @customer = Customer.find(params[:customer_id])
  @job = @customer.jobs.build
end

def edit
end


def create
  @job = Job.new(job_params)

respond_to do |format|
  if @job.save
    format.html { redirect_to customer_path(@customer), notice: 'Job was successfully created.' }
    format.json { render action: 'show', status: :created, location: @job }
  else
   format.html { render action: 'new' }
   format.json { render json: @job.errors, status: :unprocessable_entity }
  end
  end
end

def update
  respond_to do |format|
    if @job.update(job_params)
      format.html { redirect_to @job, notice: 'Job was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render action: 'edit' }
      format.json { render json: @job.errors, status: :unprocessable_entity }
    end
  end
end

def destroy
  @job.destroy
  respond_to do |format|
    format.html { redirect_to jobs_url }
    format.json { head :no_content }
  end
end

private
  # Use callbacks to share common setup or constraints between actions.
  def set_job
    @job = Job.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def job_params
    params.require(:job).permit(:box_count, :install_date)
  end
end

I receive the following error when the controller is trying to redirect after the form submission:

No route matches {:id=>nil} missing required keys: [:id]

Here is the line that the error calls out:

format.html { redirect_to customer_path(@customer), notice: 'Job was successfully created.' } 

I can see the ID being passed through in the params, but for some reason it does not like customer_path(@customer).

Here is my new job form:

<%= form_for([@customer, @job]) do |f| %>
 <% if @job.errors.any? %>
  <div id="error_explanation">
    <h2><%= pluralize(@job.errors.count, "error") %> prohibited this job from being saved:</h2>

    <ul>
    <% @job.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
    </ul>
  </div>
<% end %>

<div class="field">
  <%= f.label :box_count %><br>
  <%= f.number_field :box_count %>
</div>
<div class="field">
  <%= f.label :install_date %><br>
  <%= f.text_field :install_date %>
</div>
<div class="actions">
  <%= f.submit %>
</div>
<% end %>

Anyone know what I am doing wrong?


Solution

  • @customer is never initialized. Try this:

    def create
      @job = Job.new(job_params)
    
    respond_to do |format|
      if @job.save
        @customer = @job.customer
        format.html { redirect_to customer_path(@customer), notice: 'Job was successfully created.' }
     ... 
    

    Also your jobs_params must allow the customer_id:

      def job_params
        params.require(:job).permit(:box_count, :install_date)
        params.permit(:customer_id)
      end