나는 Ember의 초보자이며 간단한 게시물 및 댓글 응용 프로그램을 구현하려고합니다.Ember의 관련 레코드 저장
나는 레일 배경을 가지고 있으므로 레일 API를 사용하고 있습니다.
나는 자습서를 따라했으며 게시물을 저장하고 모든 주석을 가져오고 게시물을 삭제할 수 있습니다. 그러나 게시물에 관련된 의견을 저장하는 데 문제가 있습니다.
는 다음 모델에 대한 코드입니다
post.js
import DS from 'ember-data';
export default DS.Model.extend({
title: DS.attr('string'),
body: DS.attr('string'),
comments: DS.hasMany('comment')
});
comment.js
import DS from 'ember-data';
export default DS.Model.extend({
author: DS.attr('string'),
body: DS.attr('string'),
post: DS.belongsTo('post')
});
루트/게시물/댓글/new.js
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return {};
},
renderTemplate() {
this.render('post.comment.new', { into: 'application' });
},
actions: {
save() {
const post = this.modelFor('post');
const newComment = this.get('store').createRecord('comment', this.currentModel);
newComment.set('post', post);
newComment.save().then(() => {
this.transitionTo('post', post);
});
},
cancel() {
this.transitionTo('post', this.modelFor('post'));
}
}
});
router.js 내가 문제를 직면하고있어 어디 주석이 저장
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: config.locationType,
rootURL: config.rootURL
});
Router.map(function() {
this.route('posts');
this.route('post.new', { path: 'posts/new' });
this.resource('post', { path: 'posts/:post_id' }, function() {
this.route('comment.new', { path: 'comments/new' });
});
});
export default Router;
. 이건 정말 이상한하지만 주석을 저장하는 동안 서버에 전달 된 PARAMS는 내가 이해에서
Parameters: {"comment"=>{"author"=>"dsa", "body"=>"asd", "post"=>"9"}}
Unpermitted parameter: post
과 같은 매개 변수가 POST_ID 및 게시하지되어야 보인다. 게시물이 전달되면 객체이어야합니다. Ember에 대한 명확한 이해가 아직 없기 때문에 나는 틀릴 수도 있습니다. 무작위 코드 하구에
, 난 내가
post_id: DS.belongsTo('post')
로 서버에 전달 된 PARAMS을
post: DS.belongsTo('post')
의견 모델의 관계를 교체 할 경우 것으로 나타났습니다
Parameters: {"comment"=>{"author"=>"fg", "body"=>"dfs", "post_id"=>nil}}
그러나 실제로는 post_id를 해당 없음으로 전달하지 않습니다.
이것은 절대적으로 잘못되었을 수 있으며 작동 방법이 아니지만 나는 우둔합니다.
도움 주셔서 감사합니다.
감사합니다, 그것은했다. 비록 이것이 올바른 접근법인지 궁금해하고 있었지만. 나는 그러한 코드로 어떤 문서 나 예제를 찾지 못했음을 의미한다. 더 자세한 설명을 위해 참고 자료를 공유 할 수 있습니까? – Aakanksha
@Aakanksha 이에 대한 정확한 문서는 모르지만 [this] (http://emberjs.com/api/data/classes/DS.RESTSerializer.html#method_keyForRelationship).백엔드 API가 json api 표준이므로 변경할 필요가 없으며 [JSONAPISerializer] (http://emberjs.com/api/data/classes/DS.JSONAPISerializer.html)이면 충분합니다. 그러나 설명했듯이 나머지 API 및 직렬화의 백엔드는 경우마다 다를 수 있습니다. 예를 들어, 모든 관계가 낙타에 의해 처리됩니다. Ember를 사용하면 원하는 모든 파트를 사용자 정의 할 수 있습니다. AFAIK 이것은 당신의 문제에 대한 냉담한 접근법입니다. Best –
이 말이 맞습니다. 나는 이것을 위해 엠버 독 (Ember Doc)을 놓쳤을 것입니다. 참조 용으로 고맙습니다. – Aakanksha