Skip to content

Commit

Permalink
working on new_comment ajax request
Browse files Browse the repository at this point in the history
  • Loading branch information
natgit8 committed Sep 8, 2017
1 parent 1210c8e commit 364fe27
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 50 deletions.
16 changes: 8 additions & 8 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
//= require_tree .


$.ajax({
url : ('/recipes/:recipe_id/comments/:id'),
type : 'DELETE',
dataType : 'json',
success : function(e) {
alert("You just clicked delete button!");
}
});
// $.ajax({
// url : ('/recipes/:recipe_id/comments/:id'),
// type : 'DELETE',
// dataType : 'json',
// success : function(e) {
// alert("You just clicked delete button!");
// }
// });
57 changes: 36 additions & 21 deletions app/assets/javascripts/comments.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@

// $(document).ready(function() {
// $("a.delete-comment").on('click', function() {
// $.ajax({
// url: '/recipes/' + this.parentElement.id,
// type: 'DELETE',
// success: function(r) {
// alert("You clicked delete!");
// }
// });
// });
// });


// $(function(){
// $('.button_to').on('click', function(e) {
// console.log($(this))
// // const recipeId = //the data attribute of the recipe id
// // const commentId = //the data atrtribute of the comment id
// //
// // $.ajax({
// // url: `recipes/${recipeId}/comments/${commentId}`,
// // method: 'DELETE'
// // })
// alert('you clicked delete button')
// e.preventDefault();
// e.stopPropagation();
// })
// })


$(function(){
$('.button_to').on('click', function(e) {
console.log($(this))
const recipeId = //the data attribute of the recipe id
const commentId = //the data atrtribute of the comment id
$('.new_comment').on('submit', function(e) {
console.log(this)
// alert('clicked submit');
// e.preventDefault();
url = this.action
console.log(url);

$.ajax({
url: `recipes/${recipeId}/comments/${commentId}`,
method: 'DELETE'
})
alert('you clicked delete button')
e.preventDefault();
e.stopPropagation();
type: 'POST',
url: url,
data: {'comment[body]': $('#comment_body').val(),
'comment[rating]': $('#comment_rating').val(),
}
dataType: 'json',
success: function(response) {
alert('clicked submit');
e.preventDefault();
}
});

})
})
15 changes: 5 additions & 10 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class CommentsController < ApplicationController
before_action :find_recipe

def index
@comments = @recipes.comments
end
# def index
# @comments = @recipes.comments
# end

def new
@comment = Comment.new
Expand All @@ -24,12 +24,7 @@ def destroy
@recipe = Recipe.find(params[:recipe_id])
@comment = @recipe.comments.find(params[:id])
@comment.destroy
redirect_to recipe_path
# respond_to do |format|
# format.html { redirect_to(recipe_path(id: params[:recipe_id])}
# format.json { render json: @recipe }
# format.js
# end
redirect_to recipe_path(@recipe)
end

private
Expand All @@ -39,6 +34,6 @@ def find_recipe
end

def comment_params
params.require(:comment).permit(:rating, :body, :recipe_id)
params.require(:comment).permit(:id, :rating, :body, :recipe_id)
end
end
18 changes: 9 additions & 9 deletions app/controllers/recipes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ def update

def show
@recipe = Recipe.find(params[:id])
respond_to do |f|
f.html { render :show }
f.json { render json: @recipe }
end
# respond_to do |f|
# f.html { render :show }
# f.json { render json: @recipe }
# end
end

def edit
respond_to do |f|
f.html { render :edit }
f.json { render :edit }
end
end
# respond_to do |f|
# f.html { render :edit }
# f.json { render :edit }
# end
end

def favorite(_user)
@favorites << Favorite.new(user: @user)
Expand Down
1 change: 0 additions & 1 deletion app/views/comments/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<div class="container" id="comment-form">
<div class="row">
<h3>Add a comment:</h3>
<%= form_for([@recipe, @recipe.comments.build]) do |f| %>
<h4><%= f.label :rating %> (1-5)</h4>
Expand Down
1 change: 0 additions & 1 deletion app/views/comments/destroy.js.erb

This file was deleted.

2 changes: 2 additions & 0 deletions app/views/comments/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!-- <%#= @comment.body %>
<%#= @comment.rating %> -->

0 comments on commit 364fe27

Please sign in to comment.