我的Sinatra方法正确接收JSON,但由于某种原因没有保存它。它是从Angular JS发出的,所以我想知道下列其中一个是否会出现问题:
我已经发布了下面的代码,如果你有任何想法,为什么它不起作用那将是太棒了。
非常感谢。
app.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
controllers.js(Angular)
// 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中的puts的正确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>
答案 0 :(得分:1)
我认为问题是Datamapper不知道哪个属性用作主键。请尝试在下载课程中使用此功能:
property :downloadID, Serial, key: true
答案 1 :(得分:0)
我遇到了同样的问题POST并将JSON请求发送到sinatra。
我通过将其添加到我的config.ru文件来克服它。从这里得到它
require 'rack/parser'
use Rack::Parser, content_types: {
'application/json' => Proc.new { |body| ::MultiJson.decode body }
}
答案 2 :(得分: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
或类似的东西
答案 3 :(得分: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