ActiveRecord :: RecordNotFound:找不到ID = 62的模型的ID = 62的模型

时间:2018-11-06 09:02:17

标签: mysql ruby-on-rails ruby

  

找不到ID = 62的Metric :: MetricDirector的ID = 62的MetricLocationHelper

我有has_and_belongs_to_many关联,我的模型是

class MetricLocationHelper < ActiveRecord::Base
     has_and_belongs_to_many :metric_metric_directors, class_name: "Metric::MetricDirector", association_foreign_key: "metric_metric_director_id"
end

class Metric::MetricDirector < ActiveRecord::Base
   has_and_belongs_to_many :metric_location_helpers, foreign_key: 'metric_metric_director_id'

   accepts_nested_attributes_for :metric_location_helpers, reject_if: :all_blank, allow_destroy: true
end

我的第三个表格列是

  • metric_metric_director_id
  • metric_location_helper_id

在控制器中

 def metric_director_params
   params.require(:metric_metric_director).permit(:name, :username, :region, metric_location_helpers_attributes: [:id, :_destroy])
end

更新和创建记录时遇到问题。

  

ActiveRecord :: RecordNotFound位于Metric :: MetricDirectorsController#update

     

找不到ID = 62的Metric :: MetricDirector的ID = 62的MetricLocationHelper

更新代码

class Metric::MetricDirectorsController < ApplicationController
  before_action :set_metric_director, only: [:show, :edit, :update, :destroy]

def new
  @metric_director = Metric::MetricDirector.new
  @locations = @metric_director.metric_location_helpers.build
end

def edit
  @locations = @metric_director.metric_location_helpers
end

def create
  @metric_director = Metric::MetricDirector.new(metric_director_params)

  if @metric_director.save
    redirect_to metric_metric_directors_path, notice: 'Director was successfully created.'
  else
    render :new
  end
end

def update  
  if @metric_director.update(metric_director_params)
  redirect_to metric_metric_directors_path, notice: 'Director was successfully updated.'
  else
    render :edit
  end
end


private

  def set_metric_director
    @metric_director = Metric::MetricDirector.find(params[:id])
  end


def metric_director_params
   params.require(:metric_metric_director).permit(:name, :username, :region, metric_location_helpers_attributes: [:id, :_destroy])

end

非常感谢您的帮助

谢谢

0 个答案:

没有答案