Ruby on rails 为什么我的记录保存后所有字段都为零?
我正在制作一个ember和rails应用程序 我正在尝试向您发送一个post请求 我的请求是获得200分,并正在创建一个记录,但所有内容均为零: 为什么会这样Ruby on rails 为什么我的记录保存后所有字段都为零?,ruby-on-rails,ember.js,ember-data,jsonapi-rails,Ruby On Rails,Ember.js,Ember Data,Jsonapi Rails,我正在制作一个ember和rails应用程序 我正在尝试向您发送一个post请求 我的请求是获得200分,并正在创建一个记录,但所有内容均为零: 为什么会这样 #<Candidate id: 15, name: nil, email: nil, created_at: "2018-08-23 22:28:07", updated_at: "2018-08-23 22:28:07"> 我在我的js控制台中也遇到了这个错误: SyntaxError:JSON输入意外结束 我正在使用“
#<Candidate id: 15, name: nil, email: nil, created_at: "2018-08-23 22:28:07", updated_at: "2018-08-23 22:28:07">
我在我的js控制台中也遇到了这个错误:
SyntaxError:JSON输入意外结束
我正在使用“jsonapi rails”gem
以下是服务器日志和“网络”选项卡的屏幕截图:
这是我的适配器:
import DS from "ember-data";
export default DS.JSONAPIAdapter.extend({
host: "http://localhost:3000",
dataType: "json"
});
我没有在前端使用任何序列化程序。我应该是吗?
是的,我需要jsonapi数据。您必须在您的ember应用程序中定义一个模型: 此外,如果您使用的是jsonapi规范,那么应该设置
你的参数是什么样子的?这是我的参数:{controller=>candidates,action=>create}你的参数中有模型数据吗?你的JS代码没有向rails应用程序发送参数名称、电子邮件等。您也不必传递id,因为它是由rails自动创建的。这意味着params.permit:name,:email应该是params.require:candidate.permit:name,:email。从服务器日志发布传入参数可能会有所帮助。谢谢!它正在工作,我忘了添加序列化程序!
import Ember from "ember";
export default Ember.Route.extend({
model() {
// return this.get("store").findAll("candidate");
},
actions: {
createCandidate() {
console.log("create candidate");
let candidate = this.get("store").createRecord("candidate", {
id: 1,
name: "Katie",
email: "katie@gmail.com"
});
candidate.save();
}
}
});
Started POST "/candidates" for 127.0.0.1 at 2018-08-29 14:07:12 +0100
Processing by CandidatesController#create as application/vnd.api+json
create
{"controller"=>"candidates", "action"=>"create"}
{}
(2.7ms) begin transaction
↳ app/controllers/candidates_controller.rb:12
Candidate Create (0.4ms) INSERT INTO "candidates" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2018-08-29 13:07:12.984174"], ["updated_at", "2018-08-29 13:07:12.984174"]]
↳ app/controllers/candidates_controller.rb:12
(1.3ms) commit transaction
↳ app/controllers/candidates_controller.rb:12
(0.1ms) begin transaction
↳ app/controllers/candidates_controller.rb:14
(0.0ms) commit transaction
↳ app/controllers/candidates_controller.rb:14
Completed 200 OK in 9ms (Views: 0.1ms | ActiveRecord: 4.5ms)
import DS from "ember-data";
export default DS.JSONAPIAdapter.extend({
host: "http://localhost:3000",
dataType: "json"
});
// app/adapters/application.js
import DS from 'ember-data';
export default DS.JSONAPIAdapter.extend({});
// app/serializers/application.js
import DS from 'ember-data';
export default DS.JSONAPISerializer.extend({});