使用嵌套路线无法找到没有 ID 错误的锻炼
Couldn't find Workout without an ID Error With Nested Routes
我有一组嵌套路由,如下所示:
resources :workouts do
resources :exercises do
resources :reports, shallow: true
end
end
我试图让它们尽可能浅,但不嵌套它们只会产生太多错误。
现在他们工作正常,除非我尝试访问 exercises#index
页面进行特定锻炼。我正在 link 页面是这样的:
<%= link_to 'Add/Edit Exercises', workout_exercises_path(@workout, exercise), method: :index %>
当我点击 link 我得到:
ActiveRecord::RecordNotFound at /workouts/3/exercises
Couldn't find Workout without an ID
我的 exercises_controller
在指示的行上调用错误:
class ExercisesController < ApplicationController
before_action :authenticate_user!
def index
@workout = Workout.friendly.find(params[:id]) <<<<<THIS LINE
@exercise = Exercise.new
@exercises = Exercise.all
end
def new
@exercise = Exercise.new
end
def create
@workout = Workout.friendly.find(params[: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, :workout_id)
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
friendly.find
是因为我正在使用 friendly_id gem,它在 url 中生成 slug 而不是 id。我在其他工作模型中以相同的方式使用它,所以我不认为这是导致当前问题的原因。
谁能看出我为什么会收到此错误?我在我的错误的实时 shell 中输入了 @workout
,它说 @workout
是 nil
,这意味着它没有以某种方式获取值。我知道这是重要信息,但不知道如何修复它。
问题是:
您的路线是 /workouts/3/exercises
,因此格式如下:/workouts/:workout_id/exercises
所以稍微修改一下您的create
方法:
def create
@workout = Workout.friendly.find(params[:workout_id])
exercise = @workout.exercises.build(exercise_params)
...
end
def index
@workout = Workout.friendly.find(params[:workout_id])
@exercise = Exercise.new
@exercises = Exercise.all
end
试试这个,希望对你有帮助。
我有一组嵌套路由,如下所示:
resources :workouts do
resources :exercises do
resources :reports, shallow: true
end
end
我试图让它们尽可能浅,但不嵌套它们只会产生太多错误。
现在他们工作正常,除非我尝试访问 exercises#index
页面进行特定锻炼。我正在 link 页面是这样的:
<%= link_to 'Add/Edit Exercises', workout_exercises_path(@workout, exercise), method: :index %>
当我点击 link 我得到:
ActiveRecord::RecordNotFound at /workouts/3/exercises
Couldn't find Workout without an ID
我的 exercises_controller
在指示的行上调用错误:
class ExercisesController < ApplicationController
before_action :authenticate_user!
def index
@workout = Workout.friendly.find(params[:id]) <<<<<THIS LINE
@exercise = Exercise.new
@exercises = Exercise.all
end
def new
@exercise = Exercise.new
end
def create
@workout = Workout.friendly.find(params[: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, :workout_id)
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
friendly.find
是因为我正在使用 friendly_id gem,它在 url 中生成 slug 而不是 id。我在其他工作模型中以相同的方式使用它,所以我不认为这是导致当前问题的原因。
谁能看出我为什么会收到此错误?我在我的错误的实时 shell 中输入了 @workout
,它说 @workout
是 nil
,这意味着它没有以某种方式获取值。我知道这是重要信息,但不知道如何修复它。
问题是:
您的路线是
/workouts/3/exercises
,因此格式如下:/workouts/:workout_id/exercises
所以稍微修改一下您的
create
方法:def create @workout = Workout.friendly.find(params[:workout_id]) exercise = @workout.exercises.build(exercise_params) ... end
def index
@workout = Workout.friendly.find(params[:workout_id])
@exercise = Exercise.new
@exercises = Exercise.all
end
试试这个,希望对你有帮助。