我看過很多類似的帖子,但似乎無法擺脫這個record not found
錯誤,當我嘗試使用我的destroy
方法時得到的錯誤。有問題的兩個型號是workouts.rb
和exercises.rb
。鍛鍊has_many
練習。未找到軌道記錄錯誤未找到身份證找不到鍛鍊
我得到的錯誤是Couldn't find Workout without an ID
從我exercises_controller
第三線以下代碼:
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
編輯:
調用刪除代碼是:
<%= link_to "Delete #{exercise.name}", exercise_path, method: :delete, data: { confirm: 'Are you sure?' } %>
哪裏這個錯誤是來自任何想法?
什麼是'參數'來到'摧毀'行動? – Pavan
你能粘貼代碼,從哪個請求都來這裏 – Mukesh
@Mukesh,我加入了僱員再培訓局的代碼我原來的職位。 – Liz