我坚持使用JSON API,AMS,RAILS组合......
我在书中写了一些书,比如“摇滚乐与余烬”,“Ember-cli -101” 但是这里有一个场景,我需要将一些has_many / belongs_to关系与一些数据放在一起......
在我的Ember应用程序中
模型/ customer.js
export default DS.Model.extend({
name: DS.attr('string'),
contactname: DS.attr('string'),
contactno: DS.attr('string'),
othcontactno: DS.attr('string'),
othrefdetails: DS.attr('string'),
projects: DS.hasMany('project'),
});
模型/ project.js
export default DS.Model.extend({
name: DS.attr('string'),
customer: DS.belongsTo('customer' , {async:true}),
agent: DS.belongsTo('agent' , {async:true})
});
适配器/ application.js中
export default DS.JSONAPIAdapter.extend({
host: 'http://localhost:3000',
shouldReloadRecord(store, snapshot) {
return false;
},
});
串行器/ applicaiton.js
export default DS.JSONAPISerializer.extend({
});
在我的Rails api中
Rails.application.routes.draw do
resources :enquiries
resources :agents
resources :customers
resources :projects
end
class Project < ActiveRecord::Base
belongs_to :customer
#belongs_to :agent
#has_many :enquiries
accepts_nested_attributes_for :customer
end
class Customer < ActiveRecord::Base
has_many :projects
accepts_nested_attributes_for :projects
end
class ProjectSerializer < ActiveModel::Serializer
attributes :id, :name
belongs_to :customer
#has_one :agent
#has_many :enquiries
end
class CustomerSerializer < ActiveModel::Serializer
attributes :id, :name, :contactname, :contactno, :othcontactno, :othrefdetails
has_many :projects
end
初始化/ json_api.rb
ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::JsonApi
Gem文件(由于0.9版本中的一些错误,我使用的是0.10.0 rc3)
gem 'active_model_serializers' , '~> 0.10.0.rc3'
# gem 'active_model_serializers' , '~> 0.9.3'
我正在为localhost获取json响应:3000 / customers From the rails api like this
{"data":[{"id":"6","type":"customers","attributes":{"name":"Talhaqqq","contactname":"Mohammed Talha","contactno":"9744878171","othcontactno":"+9104735253585","othrefdetails":"nil"},"relationships":{"projects":{"data":[{"id":"1","type":"projects"}]}}},{"id":"10","type":"customers","attributes":{"name":"Ameer Ali qq","contactname":"Ameer Mohammed Ali","contactno":"234234","othcontactno":"439870987","othrefdetails":"othr ref detaila"},"relationships":{"projects":{"data":[]}}},{"id":"67","type":"customers","attributes":{"name":"Signlab qqq","contactname":"Sigblab contact","contactno":"2340987","othcontactno":"sjfdla98","othrefdetails":"s098-"},"relationships":{"projects":{"data":[{"id":"2","type":"projects"}]}}},{"id":"68","type":"customers","attributes":{"name":"Anwar Nazar","contactname":"Anwar","contactno":"2+87+968","othcontactno":"354+641+9","othrefdetails":"31651651"},"relationships":{"projects":{"data":[]}}},{"id":"69","type":"customers","attributes":{"name":"Cust name qqq","contactname":"asdfda","contactno":"sdfsdf","othcontactno":"sadfafd","othrefdetails":"asfdgadfg"},"relationships":{"projects":{"data":[]}}},{"id":"70","type":"customers","attributes":{"name":"dsasva","contactname":"a","contactno":"sdfvsfva","othcontactno":"asdv","othrefdetails":"vafasdfv"},"relationships":{"projects":{"data":[]}}},{"id":"71","type":"customers","attributes":{"name":"egrweg","contactname":"aevg","contactno":"asdgr","othcontactno":"aergarg","othrefdetails":"saerg"},"relationships":{"projects":{"data":[]}}}]}
我正在尝试为项目设置客户......
我在其中一个路线动作中手动尝试了....(我不知道在ember中执行此操作的最佳方法)..两本教程书都没有给我一个设置关系数据的最佳方法< / p>
let project = this.store.peekRecord('project', 3);
let customer = this.store.peekRecord('customer', 6);
project.set('customer', customer );
project.save();
我在轨道上得到这个
Parameters: {"data"=>{"id"=>"3", "attributes"=>{"name"=>"Xyy project"}, "relationships"=>{"customer"=>{"data"=>{"type"=>"customers", "id"=>"6"}}}, "type"=>"projects"}, "id"=>"3"}
我试图允许Projects_controller中的关系,属性
params.require(:data).permit!
# params.require(:data)
# .permit(:id , :attributes => [:name] , :relationships => [:customer => [:data] =>[:type] ] , :customer => [:data] )
但没有什么效果好......总是给出不允许的参数......我最终允许所有参数使用
params.require(:data).permit!
最后,当我使用此功能进行更新时
def update
@project = Project.find(params[:id])
@customer = Customer.find(6)
@project.update_attributes(:customer => params[:customer])
if @project.update(project_params)
head :no_content
else
render json: @project.errors, status: :unprocessable_entity
end
end
我在rails customer_id = NULL ..
中得到了这个响应Started PATCH "/projects/1" for 127.0.0.1 at 2015-12-09 09:32:42 +0530
Processing by ProjectsController#update as JSON
Parameters: {"data"=>{"id"=>"1", "attributes"=>{"name"=>"Lulu Mall Project qw "}, "relationships"=>{"customer"=>{"data"=>{"type"=>"customers", "id"=>"67"}}}, "type"=>"projects"}, "id"=>"1"}
Project Load (1.0ms) SELECT `projects`.* FROM `projects` WHERE `projects`.`id` = 1 LIMIT 1
CACHE (0.0ms) SELECT `projects`.* FROM `projects` WHERE `projects`.`id` = 1 LIMIT 1 [["id", "1"]]
Customer Load (0.9ms) SELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 6 LIMIT 1
(0.3ms) BEGIN
SQL (1.1ms) UPDATE `projects` SET `customer_id` = NULL, `updated_at` = '2015-12-09 04:02:42' WHERE `projects`.`id` = 1
(50.4ms) COMMIT
{"data"=>{"id"=>"1", "attributes"=>{"name"=>"Lulu Mall Project qw "}, "relationships"=>{"customer"=>{"data"=>{"type"=>"customers", "id"=>"67"}}}, "type"=>"projects"}, "controller"=>"projects", "action"=>"update", "id"=>"1"}
(0.8ms) BEGIN
我知道我完全糊涂了,我尝试使用没有JSON API标准的AMS 0.9版本,但最终出现了堆栈级别太深的错误并且出现了其他问题。
但是使用AMS 0.10.0 rc版本和标准JSON API格式,我找不到这些问题的任何教程或答案..
感觉有些事情就像日食会发生在这些事情上一起工作......