0

我的 Sinatra 方法正在正确接收 JSON,但由于某种原因没有保存它。它是从 Angular JS 路由的,所以我想知道是否存在以下问题之一:

  • 到 Sinatra 的路由不正确,但是 JSON 从 Sinatra 路由中的 put 显示
  • 数据库不接受转换后的 JSON(不知道为什么)
  • JSON 解析不正确,但是解析技术在我的应用程序的其余部分中是相同的。

我已经在下面发布了代码,如果您对它为什么不起作用有任何想法,那就太好了。

非常感谢。

应用程序.rb

#edit download
put '/view1/downloadedit' do
  @download = Download.get(1)  #1 for testing, will be downloadID
  data= JSON.parse(request.body.read)
  puts @download
  puts data
  if
    @download.update(data)
    status 201
    puts "edit saved okay"
  else
    status 201
    puts "edit failed to SAVE"
  end
end

控制器.js(角度)

  // a scope function to edit a record
  $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',
        data : result
    });
    $scope.loadData();
 };
}]);

来自终端的反馈显示 app.rb 中 put 的正确 JSON 输出

  angular connection working
  {"downloadID"=>1, "PageID"=>"1", "title"=>"nmnbm", "dlLink"=>"bnmnbm", "imgSrc"=>"mnbmnb", "caption"=>"aaa", "dlLive"=>1, "createdAt"=>nil}

下载类

#class download
class Download
include DataMapper::Resource
property :downloadID, Serial
property :PageID, String
property :title, String
property :dlLink, String
property :imgSrc, String
property :caption, String
property :dlLive, Integer
property :createdAt, DateTime
end

用于下载的 MySQL 表结构,导出为 CSV

<table_structure name="downloads">
    <field field="download_id" type="int(10) unsigned" null="NO" key="PRI" default="<null>" extra="auto_increment" />
    <field field="page_id" type="varchar(50)" null="YES" key="" default="<null>" extra="" />
    <field field="title" type="varchar(50)" null="YES" key="" default="<null>" extra="" />
    <field field="dl_link" type="varchar(50)" null="YES" key="" default="<null>" extra="" />
    <field field="img_src" type="varchar(50)" null="YES" key="" default="<null>" extra="" />
    <field field="caption" type="longtext" null="YES" key="" default="<null>" extra="" />
    <field field="dl_live" type="int(1)" null="YES" key="" default="<null>" extra="" />
    <field field="created_at" type="datetime" null="YES" key="" default="<null>" extra="" />

    <options name="downloads" engine="InnoDB" version="10" row_format="Compact" rows="8" avg_row_length="2048" data_length="16384" max_data_length="0" index_length="0" data_free="4194304" create_time="2013-11-04 17:26:56" update_time="<null>" collation="utf8_general_ci" create_options="" comment="" />
</table_structure>
4

4 回答 4

1

我认为问题在于 Datamapper 不知道将哪个属性用作主键。尝试在您的下载类中使用它:

property :downloadID, Serial, key: true
于 2013-11-10T19:38:05.230 回答
0

需要通过 :id 属性进行更新。

put '/view1/downloadedit/:id' do
  data = JSON.parse(request.body.read)
  edit_id = data[0]["downloadID"]
  p data
  p edit_id
  @download_edit = Download.get(params[:id])
  p @download_edit
  success = @download_edit.update(:title => data[0]['title'],
                                  :caption => data[0]['caption'],
                                  :dlLink => data[0]['dlLink'],
                                  :imgSrc => data[0]['imgSrc'])
  if success
    status 201
    puts 'edit saved okay'
  else
    status 201
    puts 'edit failed to SAVE'
  end
end
于 2013-12-09T20:45:45.207 回答
0

我在 POSTing 和 PUTing JSON 请求到 sinatra 时遇到了同样的问题。

我通过将它添加到我的 config.ru 文件中克服了它。从这里得到

require 'rack/parser'

use Rack::Parser, content_types: {
  'application/json' => Proc.new { |body| ::MultiJson.decode body }
}
于 2013-11-05T22:24:46.027 回答
0

还有你的代码

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

应该是

put '/view1/downloadedit' do
  # Below line shoudlnt be needed if the config.ru rack/parser works properly
  #ng_params = JSON.parse(request.body.read)

  @download = Download.where(id: params[:downloadID])

  # Maybe add an update! to blow up the update with validations to see if that is why data is not saving.
  unless @download.update(ng_params)
    return 'Error message'
  end
  @download
end

或类似的东西

于 2013-11-05T22:34:50.217 回答