Rails一对多关联,使用post ajax向许多人添加数据

时间:2016-08-17 19:59:46

标签: javascript jquery ruby-on-rails ajax parameters

我在Rails中有一对多关联。歌曲有很多频道和频道属于歌曲。我正在尝试用6个频道创建一首新歌。这是我在前端的表格:

            $.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的所有输入并且导致问题,但是当我更改名称后端时不会将其识别为允许的参数。这是我的歌曲控制器创建操作:< / p>

def create
    @song = Song.new(song_params)
    @song.save

    @ch1 = Channel.new(channel_params1)
    @ch1.song_id=@song.id
    @ch1.save


    @ch2 = Channel.new(channel_params2)
    @ch2.song_id=@song.id
    @ch2.save

    @ch3 = Channel.new(channel_params3)
    @ch3.song_id=@song.id
    @ch3.save

    @ch4 = Channel.new(channel_params4)
    @ch4.song_id=@song.id
    @ch4.save

    @ch5 = Channel.new(channel_params5)
    @ch5.song_id=@song.id
    @ch5.save

    @ch6 = Channel.new(channel_params6)
    @ch6.song_id=@song.id
    @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

注意:我的操作成功设置了一个网址,但是所有6个频道的网址都设置为最后一个表单条目,因此我为所有6个网址设置了相同的网址。

1 个答案:

答案 0 :(得分:0)

如您所推断,输入的名称不正确。我没有建立你不使用模板或Rails视图的连接。命名的技巧是在名称中包含索引:

<input id="u1" type="text" name="url[0]">    
<input id="u2" type="text" name="url[1]">

等等。 Rails会将这些字符串组合成一个字符串数组,类似于:

{ url: ["your.first.url", "your.second.url,...]} 

除此之外,还有更多的参数 - 当然 - 你也有通道包装那些东西。我没有深入了解模型的整体形状以及决定形状的内容。