Rails使用父ID的子ID

时间:2016-03-04 00:50:00

标签: ruby-on-rails activerecord has-many belongs-to nested-resources

我的问题是,当我在露营车展示页面下时

当前露营网址:

campers/1

然后我点击查看它使用camper_id的约会,这是一个错误,所以说如果camper_id是1,它将使用appointment_id作为1,实际上约会ID是3,所以它说Cann& #39;找到id为1的约会。

表标题

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

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

Campers Controller Show Action

@appointments = @camper.appointments

露营者模型

has_many :appointments, dependent: :destroy

约会模式

belongs_to :camper

浅层嵌套路线文件

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

露营控制器

 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

约会控制器

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 个答案:

答案 0 :(得分:0)

appointment_path只需要一个约会参数。删除@camper参数:

appointment_path(@appointment)