zy: delete post

This commit is contained in:
lovelyzhang 2016-12-27 17:45:57 +08:00
parent af26c41a7d
commit face9b417f
7 changed files with 47 additions and 9 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -60,8 +60,8 @@
<div class="posts">
<% @micro_posts.each do |micro_post| %>
<div class="micro-post">
<button class="close" data-dismiss="alert" type="button">×</button>
<div class="micro-post" id="<%= micro_post["postid"] %>">
<button class="close delete-post" data-dismiss="alert" type="button">×</button>
<div class="widget-container fluid-height padded">
<div class="heading col-md-12">
<i class="icon-tags"><%= micro_post["type"] %></i>
@ -79,7 +79,7 @@
</div>
<div class="col-md-12">
<div class="btn btn-default btn-block">
<div class="btn btn-default btn-block show-details">
查看详情
</div>
</div>
@ -186,4 +186,27 @@
function G(id) {
return document.getElementById(id);
}
$(".delete-post").bind("click", function () {
var errorhtml = '<div class="alert alert-danger">' +
'<button class="close" data-dismiss="alert" type="button">×</button>删除失败</div>';
var successhtml = '<div class="alert alert-success">' +
'<button class="close" data-dismiss="alert" type="button">×</button>删除成功</div>';
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);
}
}
});
});
</script>

View File

@ -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'

Binary file not shown.