【问题标题】:Rails One to Many association, adding data to many using post ajaxRails 一对多关联,使用 post ajax 向多个关联添加数据
【发布时间】:2016-08-17 19:59:45
【问题描述】:

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

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

注意:我的操作成功设置了一个 url,但是所有 6 个通道的 url 都设置为最后一个表单条目,所以我得到了所有 6 个相同的 url。

【问题讨论】:

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


    【解决方案1】:

    正如您所推断的,您输入的名称不正确。我没有说明您没有使用模板或 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,...]} 
    

    当然,params 的内容远不止这些——你也可以让频道包装这些东西。我不了解您的模型的整体形状,这就是决定该形状的原因。

    【讨论】:

    • 问题是我没有使用rails的视图部分我正在使用前端jquery并对那部分做出反应。
    • 啊,好的。这就说得通了。这里的技巧是使用名称中的索引构建输入名称。例如&lt;input id="u1" type="text" name="url[0]"&gt; Rails 会将其提取到参数中作为 url 列表,类似于 `{ "url" => [ , ...]}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多