简体   繁体   English

Rails一对多关联,使用post ajax将数据添加到多个

[英]Rails One to Many association, adding data to many using post ajax

I have a one to many association in Rails. 我在Rails中有一个一对多的协会。 Song has many channels and channel belongs to song. 歌曲有很多频道,频道属于歌曲。 I am trying to create a new song with 6 channels. 我正在尝试用6个频道创建一首新歌。 Here is my form in front end: 这是我前端的表格:

            $.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>

As you can see from my code my form field has all inputs named url and that causes the problem, but when i change the name back end does not recognise it as permitted parameter.Here is my songs-controller create action: 从我的代码中可以看到,我的表单字段具有名为url的所有输入,这会导致问题,但是当我更改名称时,后端无法将其识别为允许的参数。这是我的歌曲控制器创建动作:

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

NOTE:My action is successful to set one url, however urls of all 6 channels are set to the last form entry so I get same url for all 6. 注意:我的操作成功设置了一个URL,但是所有6个通道的URL都设置为最后一个表单条目,因此我为所有6个通道获得相同的URL。

As you've deduced, the names of your inputs are not correct. 正如您所推论的,您输入的名称不正确。 I didn't make the connection that you aren't using templates or Rails views. 我没有建立您未使用模板或Rails视图的连接。 The trick with the naming is that you include the index in the name: 命名的技巧是在名称中包括索引:

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

and so on. 等等。 Rails will group those into an array of strings for you similar to: Rails会为您将这些字符串分组为一个字符串数组,类似于:

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

There will be more to the params than that, of course--you'll have the channel wrapping that stuff as well. 当然,除了参数之外,还有更多的东西-您还将拥有包装这些东西的渠道。 I have no insight into the overall shape of your models and that's what will dictate that shape. 我对您的模型的总体形状没有任何了解,而这正是决定这种形状的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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