简体   繁体   中英

Rails Routes And AJAX Requests

I'm completely stuck right now and could really use some help.

Here is what I'm trying to do. I'm using Ruby on Rails and I have two models. One is Exercises and one is Routines. A Routine is made up of various exercises. I currently have it setup so I can drag and drop from a list of exercises into a list of exercises for the current selected routine. To model this relationship I have an exercise_routine model that matches the exercise_id and the routine_id to model that they are associated. So, what I then do is when a user hits to save changes after all their dragging I use jQuery to assemble an array of all the exercise_id's in the list.

Now is the part where I'm stuck. I then want to pass that list to the create action of the exercise_routine controller. In that create action I would then compare that list of exercises with any that already existed and create any new associations and delete those that were removed.

I have seen multiple examples and none have worked out for me. So, I have gone back to just having my array of ids that I need to pass to the controller. My question at this point is how do I successfully pass that array as a parameter to the create action, or should I send this somewhere else, of the exercise_routine controller? I think the AJAX request I was using is fine, but the routing is really throwing me for a loop.

Any help is appreciated. If you need to see any code or anything I'll update as needed.

# send an ajax request with your ids
$.ajax
  url: "routines/1"
  type: "PUT",
  data: { exercice_ids: getExerciceIds() }
  success: function() {}


# update the routine exercice ids in the controller
def update
  @routine = Routine.find(params[:id])
  @routine.exercice_ids = params[:exercice_ids]
  @routine.save
end

This should work. You might want to have another action than update .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM