重复删除链接

时间:2016-12-20 16:14:52

标签: html ruby-on-rails ruby sql-delete

在用户的个人资料页面上,用户可以创建帖子。我最近添加了删除链接,供用户删除帖子。但问题是,每个帖子都显示所有帖子的所有删除链接。

Example: So if Jimmy has 3 posts, each post will have 3 delete links

删除链接工作正常,我只需要将正确的帖子显示给它的个人帖子。

这是我的代码:

posts_controller.rb



class PostsController < ApplicationController
    
    def index
    @posts = Post.allow
    end
  
    def new 
        @post = Post.new
    end
    
    def create
        @post = Post.new(post_params)
        @post.user_id = current_user.id # assign the post to the user who created it.
        respond_to do |f|
            if (@post.save) 
                f.html { redirect_to "", notice: "Post created!" }
            else
                f.html { redirect_to "", notice: "Error: Post Not Saved." }
            end
        end
    end
    
    def destroy # users can delete their own posts
    @post = Post.find(params[:id])
    if current_user == @post.user
        @post.destroy
    end
    redirect_to root_path
    end
    
    private
    def post_params # allows certain data to be passed via form.
        params.require(:post).permit(:user_id, :content)
        
    end
    
end
&#13;
&#13;
&#13;

profile.html.erb

&#13;
&#13;
<div class="row">
    <div class="col-xs-12">
        <div class="jumbotron">
            <%= @username %>
        </div>
    </div>
</div>
<!-- row 2 -->
<div class="row">
    <div class="col-xs-3">
        <div class="panel panel-default">
            <div class="panel-body">
                avatar
            </div>
        </div>
    </div>
    <div class="col-xs-6">
        <%= render '/components/post_form' %><br>
        <% for @p in @posts %>
        <div class="panel panel-default post-panel">
          <div class="panel-body row">
            <div class="col-sm-1">
              <img src="/avatar.png" class="post-avatar" height="50px" width="50px"></img>
            </div>
            <div class="col-sm-11">
              <p class="post-title"><span class="post-owner"><a href="/user/<%= User.find(@p.user_id).username %>"><%= User.find(@p.user_id).name %></a></span> <span class="post-creation">- <%= @p.created_at.to_formatted_s(:short) %></span></p>
              <p class="post-content"><%= @p.content %></p>
            </div>
            <div class="col-sm-12">
              <p class="post-links">
                <span class="glyphicon glyphicon-comment g-links" aria-hidden="true"></span>
                <span class="glyphicon glyphicon-retweet g-links" aria-hidden="true"></span>
                <span class="glyphicon glyphicon-heart g-links" aria-hidden="true"></span>
                <span class="glyphicon glyphicon-option-horizontal g-links" aria-hidden="true"></span>
                <% if user_signed_in? %>
                <% @posts.each do |post| %>

                <%= link_to 'Delete', post, method: :delete, data: { confirm: 'Are you sure?' } %>

                <% end %>
                <% end %>
            </p>
            </div>
          </div>
        </div>
        <% end %>
    </div>
    <div class="col-xs-3">
        <div class="panel panel-default">
            <div class="panel-body">
                <p>who to follow</p>
            </div>
        </div>
        <div class="panel panel-default">
            <div class="panel-body">
                <p>trends</p>
          </div>
        </div>
    </div>
</div>
&#13;
&#13;
&#13;

我该怎么做才能解决这个问题?提前谢谢!

2 个答案:

答案 0 :(得分:0)

如我所见,你有@post的2个循环, 1. for @p in @posts 2. @post.each do |post| 每个循环都在for循环中

答案 1 :(得分:0)

问题出在profile.html.erb

<% if user_signed_in? %>
  <% @posts.each do |post| %>
    <%= link_to 'Delete', post, method: :delete, data: { confirm: 'Are you sure?' } %>
  <% end %>
<% end %>

删除@posts.each循环,并将其替换为:

<%= link_to 'Delete', @p, method: :delete, data: { confirm: 'Are you sure?' } %>