【问题标题】:Rails Child id using parent idRails 子 ID 使用父 ID
【发布时间】:2016-03-04 00:50:00
【问题描述】:

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

当前露营者网址:

campers/1

然后我去点击查看它使用 camper_id 作为约会 ID 的约会,这是错误的,所以如果 camper_id 为 1,它将使用约会 ID 作为 1,实际上约会 ID 为 3,所以它说可能'找不到 id 为 1 的约会。

表格标题

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

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

Campers Controller 显示动作

@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

【问题讨论】:

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


    【解决方案1】:

    appointment_path 只接受一个约会参数。删除@camper 参数:

    appointment_path(@appointment)
    

    【讨论】:

    • 我的资源很浅,所以camper_appointment_path 不起作用
    • 我得到No route matches {:action=&gt;"show", :controller=&gt;"appointments", :id=&gt;nil} missing required keys: [:id]
    • 我为客户/露营者设置了完全相同的设置,它完美无瑕,我尝试将其复制到露营者/约会中,但它只是不想工作
    • 你觉得会怎样?
    • 没有足够的信息继续。你能发布你的控制器代码吗?
    猜你喜欢
    • 1970-01-01
    • 2020-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多