将多模型表单从 Angular 更新为 Sinatra

Updating multi-model form from Angular to Sinatra

本文关键字:更新 Sinatra Angular 模型 表单      更新时间:2023-09-26

我目前在 Angular 中更新表单并将更新推送到 Sinatra 时遇到问题。

它应该:

  • 单击时,将显示用于编辑当前项目的表单(显示项目范围内的每个字段的当前数据)。
  • 提交后,它正在尝试更新到其他范围 (updateinfo)。我不确定,但我是否需要一种使用多作用域或一个作用域的方法来允许它更新?
  • 目前脚本发送正确的 downloadID 参数,但我认为提交的范围内的 JSON 不正确。
  • 另外,我不确定Sinatra app.rb语法是否正确,对于这些框架的新手来说,很难在网上找到有用的文档。

如果有人能帮忙,将不胜感激。

下载.html

<div ng-show="showEdit">
                <form ng-submit="updateinfo(item.downloadID); showDetails = ! showDetails;">
                    <div class="input-group"><label name="title">Title</label><input type="text"
                                                                                     ng-model="item.title"
                                                                                     value="{{item.title}}"/></div>
                    <div class="input-group"><label name="caption">Download caption</label><input type="text"
                                                                                                  ng-model="item.caption"
                                                                                                  value="{{item.caption}}"/>
                    </div>
                    <div class="input-group"><label name="dlLink">Download link</label><input type="url"
                                                                                              ng-model="item.dlLink"
                                                                                              value="{{item.dlLink}}"/>
                    </div>
                    <div class="input-group"><label name="imgSrc">Image source</label><input type="url"
                                                                                             ng-model="item.imgSrc"
                                                                                             value="{{item.imgSrc}}"/>
                    </div>

                    <!-- download live input types need to be parsed as integers to avoid 500 internal server error   -->
                    <div class="input-group"><label name="imgSrc">
                        <label name="dlLive">Download live</label><input type="radio" ng-model="download.dl_live"
                                                                         value="1"/>
                        <label name="dlLive">Not live</label><input type="radio" ng-model="download.dl_live"
                                                                    value="0"/></div>
                    <div class="input-group"><label name="imgSrc"><input type="submit"/></div>
                </form>

控制器.js

$scope.loadData = function () {
    $http.get('/view1/downloadData').success(function (data) {
        $scope.items = data;
    });
};
$scope.loadData();
$scope.updateinfo = function(downloadID) {
    id = downloadID
    var result = $scope.items.filter(function( items ) {
        return items.downloadID == id;
    });
    console.log(result);
    updatedata = $scope.items
    $http({
        method : 'PUT',
        url :  '/view1/downloadedit/:downloadID',
        data : result
    });
    };

app.rb

#edit download
put '/view1/downloadedit' do
puts 'angular connection working'
ng_params = JSON.parse(request.body.read)
puts ng_params
@download = Download.update(ng_params)
end

尝试使用错误的范围。将范围更正为项目后,将路由正确的 JSON:

$scope.updateinfo = function(downloadID) {
id = downloadID
var result = $scope.items.filter(function( items ) {
    return items.downloadID == id;
});
console.log(result);
updatedata = $scope.items
$http({
    method : 'PUT',
    url :  '/view1/downloadedit/:downloadID',
    data : result
});