Please forgive me if this is a simple question. I've spent some time searching SO and have not fund a clear answer for this question. I'm also new to rails.
I have a regular old many to many relationship with an associative table in between. The relationships are A workout has many exercises An exercise belongs to many workouts.
I have 2 forms, one to create new workouts, and another to create new exercises. The 3rd form will be used to populate the associative table "workout_exercises". This will allow users to add exercises to multiple workouts.
I want a user to pick a workout, where they will be presented with a list of exercises filtered by a category drop down (which I have working). a checkbox selected on the form submission will grab the id's submitted for the exercises. The problem is the ID for the workout is not available from this form.
My models look like this
class Exercise < ActiveRecord::Base
belongs_to :category
belongs_to :workout_exercise
end
class Workout < ActiveRecord::Base
belongs_to :workout_exercise
end
class WorkoutExercise < ActiveRecord::Base
has_many :workouts
has_many :exercises
end
controller action looks like this
def exercises
@workout_exercise = WorkoutExercise.new
respond_to do |format|
format.js {
@workout = #?? I don't have the workout ID available from the request
@exercises = Exercise.where(category_id: params[:id]) #this is for the dropdown filter :id grabed on select change
}
end
end
I was thinking a possible option to this problem might be to use a nested route somehow?
Thanks in advance for any help!