Skip to content

Commit

Permalink
Fixing API Like errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tclaus committed Feb 11, 2021
1 parent 487f83f commit cdef7a2
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
7 changes: 6 additions & 1 deletion app/assets/javascripts/app/collections/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@ app.collections.Comments = Backbone.Collection.extend({

make : function(text) {
var self = this;
var comment = new app.models.Comment({ "text": text });
var comment = new app.models.Comment({ "text": text },{ post: this.post});

var deferred = comment.save({}, {
url: "/posts/"+ this.post.id +"/comments",
success: function() {
comment.set({author: app.currentUser.toJSON(), parent: self.post });

// Need interactions after make
comment.interactions = new app.models.Post.LikeInteractions(
_.extend({comment: comment, post: self.post}, comment.get("interactions"))
);
self.add(comment);
}
});
Expand Down
4 changes: 2 additions & 2 deletions app/assets/javascripts/app/models/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
app.models.Comment = Backbone.Model.extend({
urlRoot: "/comments",

initialize: function() {
this.post = this.collection.post;
initialize: function(model, options) {
this.post = options.post || this.collection.post;
this.interactions = new app.models.Post.LikeInteractions(
_.extend({comment: this, post: this.post}, this.get("interactions"))
);
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/likes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def create
post = post_service.find!(params.require(:post_id))
raise ActiveRecord::RecordInvalid unless post.public? || private_read?

like_service.create(params[:post_id])
like_service.create_for_post(params[:post_id])
rescue ActiveRecord::RecordInvalid => e
if e.message == "Validation failed: Target has already been taken"
return render_error 409, "Like already exists"
Expand Down

0 comments on commit cdef7a2

Please sign in to comment.