多くの類似した投稿を見ましたが、destroy
メソッドを使用しようとしたときにこのrecord not found
エラーを取り除くことはできません。問題の2つのモデルはworkouts.rb
とexercises.rb
です。運動has_many
エクササイズ。Railsレコードが見つかりませんエラーIDなしでワークアウトが見つかりませんでした
私は取得していますエラーが私のexercises_controller
から以下のコードの3行目にCouldn't find Workout without an ID
です:
def destroy
@workout = Workout.friendly.find(params[:workout_id])
exercise = @workout.exercises.find(params[:id])
マイexercise.rb
モデルは次のとおりです。
class Exercise < ActiveRecord::Base
belongs_to :workout
belongs_to :user
has_many :reports
validates :user, presence: true
end
私workout.rb
モデルは次のとおりです。
class Workout < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: :slugged
belongs_to :user
has_many :exercises
has_many :reports
validates :user, presence: true
end
そして私のフルexercises_controller
は次のとおりです。
class ExercisesController < ApplicationController
before_action :authenticate_user!
def index
@exercises = Exercise.all
end
def new
@exercise = Exercise.new
end
def create
@workout = Workout.friendly.find(params[:workout_id])
exercise = @workout.exercises.new(exercise_params)
exercise.user = current_user
if exercise.save
flash[:notice] = "Results saved successfully."
redirect_to [@workout]
else
flash[:alert] = "Results failed to save."
redirect_to [@workout]
end
end
def destroy
@workout = Workout.friendly.find(params[:workout_id])
exercise = @workout.exercises.find(params[:id])
if exercise.destroy
flash[:notice] = "Exercise was deleted successfully."
redirect_to [@workout]
else
flash[:alert] = "Exercise couldn't be deleted. Try again."
redirect_to [@workout]
end
end
private
def exercise_params
params.require(:exercise).permit(:name, :needs_seconds, :needs_weight, :needs_reps)
end
def authorize_user
exercise = Exercise.find(params[:id])
unless current_user == current_user.admin?
flash[:alert] = "You do not have permission to create or delete an exercise."
redirect_to [exercise.workout]
end
end
end
私のルートは単純です:
resources :workouts
resources :exercises
EDIT:
コードの削除を呼び出すには、次のとおりです。
<%= link_to "Delete #{exercise.name}", exercise_path, method: :delete, data: { confirm: 'Are you sure?' } %>
このエラーがから来ているすべてのアイデア?
'destroy'アクションに来る' params'とは何ですか? – Pavan
ここに来ているリクエストのコードを貼り付けることができます – Mukesh
@Mukesh、元の投稿にERBコードを追加しました。 – Liz