Still stuck in the basics , with the JSON API , AMS, RAILS....
I covered ember with some books like "Rock and Roll with ember js" , "Ember-cli" But here is a scenario where i need to put some has_many / belongs_to relationship with some data...
In My Ember Application
models/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'),
});
models/project.js
export default DS.Model.extend({
name: DS.attr('string'),
customer: DS.belongsTo('customer' , {async:true}),
agent: DS.belongsTo('agent' , {async:true})
});
adapters/application.js
export default DS.JSONAPIAdapter.extend({
host: 'http://localhost:3000',
shouldReloadRecord(store, snapshot) {
return false;
},
});
serializers/applicaiton.js
export default DS.JSONAPISerializer.extend({
});
And in my Rails api
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
initializers/json_api.rb
ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::JsonApi
Gem File (I am using 0.10.0 rc3 due to some bugs in 0.9 version)
gem 'active_model_serializers' , '~> 0.10.0.rc3'
# gem 'active_model_serializers' , '~> 0.9.3'
I am getting a json response for localhost: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":[]}}}]}
I am trying to set a customer for a project....
I tried it manually in one of the routes actions.... (I dont know the best way to do it in ember ).. None of the tutorial books tells about setting relationship data
let project = this.store.peekRecord('project', 3);
let customer = this.store.peekRecord('customer', 6);
project.set('customer', customer );
project.save();
And i am getting this on rails
Parameters: {"data"=>{"id"=>"3", "attributes"=>{"name"=>"Xyy project"}, "relationships"=>{"customer"=>{"data"=>{"type"=>"customers", "id"=>"6"}}}, "type"=>"projects"}, "id"=>"3"}
I tried to permit the relationships, attributes in Projects_controller
params.require(:data).permit!
# params.require(:data)
# .permit(:id , :attributes => [:name] , :relationships => [:customer => [:data] =>[:type] ] , :customer => [:data] )
But nothing worked well .. always given unpermitted parameter... and i ended up permitting all params using
params.require(:data).permit!
And finally , When i did the update using this function
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
I got this response in 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
I know i am totally confused,, I tried to use AMS 0.9 version without the JSON API Standard , but ended up in stack level too deep error and come other problems.
But using the AMS 0.10.0 rc version and standard JSON API Format, i couldn't find any tutorials or answers for these questions..
- How to set relationship data in ember ?
- The best json format from API (standart json / json with embed ids )?
- How to allow strong parametes in Rails (Especially nested with relationships{id, type} etc... With STANDARD JSON API FORMAT)?
- Update relationship (update attributes ) in Rails with the json api request from ember?
- Is it best to go with the JSON API FORMAT , and AMS ...AND JSON API ADAPTER /SERIALIZER...
It feels some thing like an eclipse should happen with these things to work all together...
Aucun commentaire:
Enregistrer un commentaire