我查看了很多类似的帖子,但似乎无法摆脱我在尝试使用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?' } %>
出现此错误的任何想法?
答案 0 :(得分:1)
如果没有代码,我们无法为您做很多事情。但是,常见的错误是忘记路线上的:id
。
看到您使用Rails自动生成它们,因此很容易忘记对您的exercice资源的销毁操作将是DELETE
/exercises/:id
操作
注意路线中的id
!
此外,读取您的控制器,看起来您期望workout_id
,那么正确的路由将是:
resources :workouts do
resources :exercices
end
这将使这些路线成为可能:
GET /workouts/:workout_id/exercices(.:format) exercices#index
POST /workouts/:workout_id/exercices(.:format) exercices#create
GET /workouts/:workout_id/exercices/new(.:format) exercices#new
GET /workouts/:workout_id/exercices/:id/edit(.:format) exercices#edit
GET /workouts/:workout_id/exercices/:id(.:format) exercices#show
PATCH /workouts/:workout_id/exercices/:id(.:format) exercices#update
PUT /workouts/:workout_id/exercices/:id(.:format) exercices#update
DELETE /workouts/:workout_id/exercices/:id(.:format) exercices#destroy
GET /workouts(.:format) workouts#index
POST /workouts(.:format) workouts#create
GET /workouts/new(.:format) workouts#new
GET /workouts/:id/edit(.:format) workouts#edit
GET /workouts/:id(.:format) workouts#show
PATCH /workouts/:id(.:format) workouts#update
PUT /workouts/:id(.:format) workouts#update
DELETE /workouts/:id(.:format) workouts#destroy
注意嵌套路线中的workout_id
。
因此,如果您使用id = 3
进行锻炼,对id = 23
进行锻炼,您就可以在DELETE
和/workouts/3/exercices/23
上发送ExercicesController
{1}},您可以访问这两个值:
params[:id] = 23
params[:workout_id] = 3
我想,就像你期望的那样。
我太过分了,希望它很有用。我试图根据你的代码丰富我的答案。