Creating multiple records in a HABTM relationship using a collection_select - Rails

半城伤御伤魂 提交于 2019-12-06 14:41:47

So the answer to this is to edit the update action in the controller with this:

workout_id = params[:group].delete(:workout_ids)

    # Adding a workout
    if workout_id
      workout = Workout.find(workout_id)
      @group.workouts << workout
    end

The above code creates a local variable workout_id that takes the parameters :group and :workout_id from within :group the .delete method removes the second parameter for updating the actual group when you change the name of the group, for example.

Then we simply push a new workout onto @group.workouts, creating a new record in the join table every time we add a new workout to the group from the collection select.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!