-1

My issue is, when I am under the camper show page

Current Camper URL:

campers/1

and I go to click on to view the appointment it uses the camper_id for the appointment_id which is wrong so say if the camper_id is 1 it will use the appointment_id as 1 and actually the appointment id is 3, so then it says Couldn't find appointment with id of 1.

Table Header

<% @appointments.each do |app| %>

<%= link_to app.camper.camperName, appointment_path(@camper, @appointment) %>

Campers Controller Show Action

@appointments = @camper.appointments

Camper Model

has_many :appointments, dependent: :destroy

Appointment Model

belongs_to :camper

Shallow Nested Routes File

resources :customers, shallow: :true do
  resources :campers do
    resources :appointments do
      resources :orders do
        member do
          patch :complete
        end
      end
    end
  end
end

Camper Controller

 class CampersController < ApplicationController
  before_action :set_camper, only: [:show, :edit, :update, :destroy]
  # before_action :set_customer, only: [:index, :new, :edit, :create, :update]
  load_and_authorize_resource

  # GET /campers
  # GET /campers.json
  def index
    @campers = @customer.campers
  end

  def list
    query = params[:q].presence || ""
    @campers = Camper.search(query, page: params[:page], per_page: 20, order: {created_at: :desc} )

  end

  # GET /campers/1
  # GET /campers/1.js
  def show
    @appointments = @camper.appointments
    respond_to do |format|
      format.html
      format.json
    end
  end

  # GET /campers/new
  def new
    @customer = Customer.find(params[:customer_id])
    @camper = @customer.campers.build
  end

  # GET /campers/1/edit
  def edit
  end

  def page_name
    "Campers"
  end


  # POST /campers
  # POST /campers.json
  def create
    @camper = Camper.new(camper_params)

    respond_to do |format|
      if @camper.save
        format.html { redirect_to camper_path(@camper), notice: 'Camper was successfully created.' } 
        format.json { render :show, status: :created, location: @camper }
      else
        format.html { render :new }
        format.json { render json: @camper.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /campers/1
  # PATCH/PUT /campers/1.json
  def update
    respond_to do |format|
      if @camper.update(camper_params)
        format.html { redirect_to camper_path(@camper), notice: 'Camper was successfully updated.' }
        format.json { render :show, status: :ok, location: @camper }
      else
        format.html { render :edit }
        format.json { render json: @camper.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /campers/1
  # DELETE /campers/1.json
  def destroy
    @camper.destroy
    respond_to do |format|
      format.html { redirect_to root_path, notice: 'Camper was successfully deleted.' }
      format.json { head :no_content }
    end
  end

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


    # Never trust parameters from the scary internet, only allow the white list through.
    def camper_params
      params.require(:camper).permit(:order_id, :customer_id, :year, :manufacturer, :modelName, :camperClass, :vin, :mileage, :notes, :user_id)
    end
end

Appointments Controller

class AppointmentsController < ApplicationController
  before_action :set_appointment, only: [:show, :edit, :update, :destroy]
  # GET /appointments
  # GET /appointments.json
  def index
    @camper = Camper.find(params[:camper_id])
    @appointments = @camper.appointments
  end

  # GET /appointments/1
  # GET /appointments/1.json
  def show
    @orders = @appointment.orders
  end

  # GET /appointments/newå
  def new
    @camper = Camper.find(params[:camper_id])
    @appointment = @camper.appointments.build
  end

  # GET /appointments/1/edit
  def edit
  end

  # POST /appointments
  # POST /appointments.json
  def create
    @appointment = Appointment.new(appointment_params)

    respond_to do |format|
      if @appointment.save
        format.html { redirect_to appointment_path(@appointment), notice: 'Appointment was successfully created.' }
        format.json { render :show, status: :created, location: @appointment }
      else
        format.html { render :new }
        format.json { render json: @appointment.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /appointments/1
  # PATCH/PUT /appointments/1.json
  def update
    respond_to do |format|
      if @appointment.update(appointment_params)
        format.html { redirect_to @appointment, notice: 'Appointment was successfully updated.' }
        format.json { render :show, status: :ok, location: @appointment }
      else
        format.html { render :edit }
        format.json { render json: @appointment.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /appointments/1
  # DELETE /appointments/1.json
  def destroy
    @appointment.destroy
    respond_to do |format|
      format.html { redirect_to camper_appointments_path(@appointment), notice: 'Appointment was successfully deleted.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def appointment_params
      params.require(:appointment).permit(:customer_id, :camper_id, :order_id, :title, :description, :date_in, :date_out)
    end
end

1 Answers1

0

appointment_path only takes a single appointment argument. Remove the @camper argument:

appointment_path(@appointment)
infused
  • 24,000
  • 13
  • 68
  • 78