From face9b417f990f8c9511e04483e344ba52a9f3a9 Mon Sep 17 00:00:00 2001 From: lovelyzhang Date: Tue, 27 Dec 2016 17:45:57 +0800 Subject: [PATCH] zy: delete post --- app/controllers/micro_posts_controller.rb | 12 +++++++++ app/helpers/micro_posts_helper.rb | 1 + app/models/micro_post.rb | 2 +- app/models/user.rb | 4 +-- app/views/micro_posts/show.html.erb | 29 +++++++++++++++++++--- config/routes.rb | 8 +++--- db/development.sqlite3 | Bin 40960 -> 40960 bytes 7 files changed, 47 insertions(+), 9 deletions(-) diff --git a/app/controllers/micro_posts_controller.rb b/app/controllers/micro_posts_controller.rb index 9edfd0d..34061c2 100644 --- a/app/controllers/micro_posts_controller.rb +++ b/app/controllers/micro_posts_controller.rb @@ -34,4 +34,16 @@ class MicroPostsController < ApplicationController redirect_to microposts_path end + def delete + @user = current_user + micropost_id = params[:micropost_id].to_i + micropost_id = @user.micro_posts.find_by(id: micropost_id) + if micropost_id + micropost_id.destroy + render json: {status:true} + else + render json: {status:false} + end + end + end diff --git a/app/helpers/micro_posts_helper.rb b/app/helpers/micro_posts_helper.rb index 53ed12d..9b5c3f2 100644 --- a/app/helpers/micro_posts_helper.rb +++ b/app/helpers/micro_posts_helper.rb @@ -9,6 +9,7 @@ module MicroPostsHelper if !micro_posts.empty? micro_posts.each do |micro_post| x = Hash.new() + x["postid"] = micro_post.id x["title"] = micro_post.title x["content"] = micro_post.content case micro_post.post_type diff --git a/app/models/micro_post.rb b/app/models/micro_post.rb index 2a2be7e..2ece845 100644 --- a/app/models/micro_post.rb +++ b/app/models/micro_post.rb @@ -1,4 +1,4 @@ class MicroPost < ApplicationRecord - belongs_to :user, dependent: :destroy + belongs_to :user has_many :comments, class_name: 'Comment', foreign_key: 'micro_post_id', dependent: :destroy end diff --git a/app/models/user.rb b/app/models/user.rb index f387ea9..91adcdc 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -5,8 +5,8 @@ class User < ApplicationRecord self.picurl = 'images/avatars/default/avatar.png' end - has_many :send_messages, class_name: 'Message', foreign_key: 'send_user' - has_many :recieve_messages, class_name: 'Message', foreign_key: 'recieve_user' + has_many :send_messages, class_name: 'Message', foreign_key: 'send_user', dependent: :destroy + has_many :recieve_messages, class_name: 'Message', foreign_key: 'recieve_user', dependent: :destroy has_many :micro_posts, dependent: :destroy has_many :comments, class_name: 'Comment', foreign_key: 'user_id', dependent: :destroy diff --git a/app/views/micro_posts/show.html.erb b/app/views/micro_posts/show.html.erb index 7132696..e783f00 100644 --- a/app/views/micro_posts/show.html.erb +++ b/app/views/micro_posts/show.html.erb @@ -60,8 +60,8 @@
<% @micro_posts.each do |micro_post| %> -
- +
"> +
<%= micro_post["type"] %> @@ -79,7 +79,7 @@
-
+
查看详情
@@ -186,4 +186,27 @@ function G(id) { return document.getElementById(id); } + + + $(".delete-post").bind("click", function () { + var errorhtml = '
' + + '删除失败
'; + var successhtml = '
' + + '删除成功
'; + var micropost_id = $(this).parent().attr("id"); + $.ajax({ + type: 'get', + dataType: 'json', + data: {micropost_id: micropost_id}, + url: '/deletepost', + success: function (data, textStatus, jqXHR) { + if(data['status'] == true) { + $('.posts').before(successhtml); + } + else { + $('.posts').before(errorhtml); + } + } + }); + }); \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 4588907..183882f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,10 +7,10 @@ Rails.application.routes.draw do get 'logout', to: 'login#logout' get '/signup', to: 'users#new' - get '/profile' ,to: 'users#edit' + get '/profile', to: 'users#edit' get '/main', to: 'main#show' - get '/activity',to: 'main#activity' + get '/activity', to: 'main#activity' get '/chat', to: 'chat#index' post '/newmsg', to: 'chat#new' @@ -19,7 +19,9 @@ Rails.application.routes.draw do get '/online', to: 'chat#online' get '/microposts', to: 'micro_posts#show' - post 'newpost', to: 'micro_posts#new' + post '/newpost', to: 'micro_posts#new' + get '/deletepost', to: 'micro_posts#delete' + get '/comments', to: 'comments#get' post '/newcomment', to: 'comments#new' diff --git a/db/development.sql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