如何删除与用户关联的一个帖子?

时间:2016-11-28 17:27:23

标签: ruby-on-rails

点击'删除帖子'在all.html.erb视图中链接,删除与特定用户关联的所有帖子。为了澄清,如果我已登录并尝试删除我的一个帖子,我的所有帖子都会被删除。

posts_controller.rb:

def destroy
    @post = Post.find(params[:id])
    @post.destroy
    redirect_to '/'
    flash[:alert] = 'Post was deleted.'
end

视野中的链接:

<div class="col-sm-10">
    <center><h3>Recent Posts</h3></center>
    <% @posts.each do |post| %>
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title"><%= post.title %></h3>
            </div>
            <div class="panel-body">
                <%= post.content %>
            </div>
            <div class="panel-footer">
                By: <%= link_to User.find(post.user_id).name, profile_path(User.find(post.user_id).name) %>
                <% if session[:user_id] == post.user_id %>
                | <%= link_to 'Delete Post', deletepost_path(post.id), method: :delete %>
                <% end %>
            </div>
        </div>    
    <% end %>    
</div>

如果相关,则为Routes.rb:

 get 'newpost', to: 'posts#new', as: 'newpost'
 post 'newpost', to: 'posts#create'
 delete 'deletepost/:id', to: 'posts#destroy', as: 'deletepost'

0 个答案:

没有答案