私は1対1の関連をRailsに持っています。ソングには多くのチャンネルがあり、チャンネルはソングに属しています。私は6チャンネルの新しい曲を作ろうとしています。ここに私のフォームは、フロントエンドである:Rails 1対多の関連付け、ポストajaxを使用して多くの人にデータを追加
$.ajax({
type : "POST",
url: ,url
dataType : 'json',
data : $("#form").serialize(),
success : function(data) {
var newSong = new Object();
newSong.song_name = data.sn;
newSong.singer_name = data.ss;
newSong.genre = data.sg;
newSong.channels[0].url=data.u1;
newSong.channels[1].url=data.u2;
newSong.channels[2].url=data.u3;
newSong.channels[3].url=data.u4;
newSong.channels[4].url=data.u5;
newSong.channels[5].url=data.u6;
},
error : function(result) {
console.log(result);
},})});});
</script>
</head>
<body>
<form id="form" action="" method="post">
Song Name: <input id="sn" type="text" name="song_name"><br><br>
Singer Name: <input id="ss" type="text" name="singer_name"><br><br>
Genre: <input id="sg" type="text" name="genre"><br><br>
Url 1: <input id="u1" type="text" name="url"><br><br>
Url 2: <input id="u2" type="text" name="url"><br><br>
Url 3: <input id="u3" type="text" name="url"><br><br>
Url 4: <input id="u4" type="text" name="url"><br><br>
Url 5: <input id="u5" type="text" name="url"><br><br>
Url 6: <input id="u5" type="text" name="url"><br><br>
<input id="submit" type="button" name="submit" value="submit">
</form>
あなたは私のコードからわかるように、私のフォームフィールドは、URLという名前のすべての入力を持っており、それが問題になるが、私は最後の名前をバックに変更する場合が許可としてそれを認識しません。
def create
@song = Song.new(song_params)
@song.save
@ch1 = Channel.new(channel_params1)
@[email protected]
@ch1.save
@ch2 = Channel.new(channel_params2)
@[email protected]
@ch2.save
@ch3 = Channel.new(channel_params3)
@[email protected]
@ch3.save
@ch4 = Channel.new(channel_params4)
@[email protected]
@ch4.save
@ch5 = Channel.new(channel_params5)
@[email protected]
@ch5.save
@ch6 = Channel.new(channel_params6)
@[email protected]
@ch6.save
respond_to do |format|
if @ch6.save
format.html { redirect_to @song, notice: 'Song was successfully created.' }
format.json { render :show, status: :created, location: @song }
else
format.html { render :show }
format.json { render json: @song.errors, :status => :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def song_params
params.permit(:song_name, :singer_name , :genre)
end
def channel_params1
params.permit(:url)
end
def channel_params2
params.permit(:url)
end
def channel_params3
params.permit(:url)
end
def channel_params4
params.permit(:url)
end
def channel_params5
params.permit(:url)
end
def channel_params6
params.permit(:url)
end
end
注:parameter.Hereは私の歌コントローラがアクションを作成することです私のアクションは1つのURLを設定することに成功している、しかし、すべての6つのチャンネルのURLが最後のフォームのエントリに設定されているので、私は、すべての6のために同じURLを取得します
問題はレールの表示部分を使用していないことです。フロントエンドjqueryを使用していて、その部分に反応しています。 –
ああ、大丈夫です。それは理にかなっている。ここでのトリックは、名前にインデックスINを持つ入力名を作成することです。たとえば、 '' Railsは、 '{" url "=> [、 ...]} –
jaydel