私は一般的にレールとウェブプログラミングを使いこなしており、これに対する答えをSOで検索しましたが、探しているものが見つかりませんでした。これは私の最初のレールプロジェクトの一つですので、どうか簡単に行ってください。 :)複数のチェックボックスをparamsハッシュで選択する
私は、エクササイズテーブルとエクササイズテーブルの多対多の関係を解決するジョインテーブルを更新しようとしています。
は、私は私の選択ボックスの提出から受けていparamsハッシュは、この
私はそれらのparams内のIDにアクセスしようとしているように見えますが、成功しません。私は私のモデルがこの
class Exercise < ActiveRecord::Base
belongs_to :muscle_group
has_many :workout_exercises
has_many :workouts, through: :workout_exercises
end
class Workout < ActiveRecord::Base
has_many :workout_exercises
has_many :exercises, through: :workout_exercises
accepts_nested_attributes_for :workout_exercises
end
class WorkoutExercise < ActiveRecord::Base
belongs_to :exercise
belongs_to :workout
end
のように見えると、フォームがこの
ようになりますdef edit
@workout = Workout.find(params[:id])
@workoutExercise = WorkoutExercise.new
respond_to do |format|
format.html
format.js {@exercise = Exercise.where(muscle_group_id: params[:idmg])}
end
end
def update
@workout = Workout.find(params[:id])
params[:workout_exercises[:exercises]].each do |e|
if !e.empty?
@workout.workoutexercise.build(:exercise_id => e)
end
end
このように私のWorkoutsControllerが見え 「整数へのシンボルのいない暗黙の型変換」エラーを得続けます
<h3>here is where our exercises will go</h3>
<% if [email protected]? %>
<%= form_for(@workout) do |w| %>
<%= fields_for :workout_exercises, @workout_exercises do |we| %>
<%= we.label :exercises %><br>
<%= we.collection_check_boxes(:exercises, @exercise, :id, :name) do |ec| %>
<%= ec.object.name%>
<%= ec.check_box %><br>
<% end %>
<% end %>
<%= w.submit "Add", class: "pure_button" %>
<% end %>
<% end %>
WorkoutControllerがtを更新する場合は、編集と更新の方法を使用しています彼はworkout_exercisesテーブル。アイデアは、現在のワークアウトのIDと、チェックボックスコレクションから渡された各IDのIDを使って、結合テーブルにデータを入れることです。
上記のフォームは、WorkoutControllerの編集アクションのajax呼び出しでレンダリングされ、 "ボディパート"のドロップダウンに基づいてチェックボックスの選択を更新する部分です。 (それは筋肉グループに関連するすべての練習を表示します)
また、より良い方法を行う方法に関するアドバイスもありがとうございます。ありがとうございます。もう一度これは私の最初のプロジェクトですので、他のヒントをいただければ幸いです。