2016-03-28 28 views
1

我对Rails很新。我完成了一篇教程here,该教程将指导您使用它创建博客的步骤。如何限制某人在Rails中删除自己的评论?

本教程的一部分向您展示了如何创建一个允许用户向文章添加注释的控制器。我试图修改它,以便用户只能删除自己的评论(而不是其他人的评论)。

问:
是否有修改代码,这样,您就可以限制用户删除自己的意见的方式?任何资源/教程也欢迎。我真的不知道如何开始。

我觉得正确的做法是在用户提交评论时以某种方式标记用户。将该信息保存在数据库中,然后在有人去删除评论时检查该信息。但我想不出一种方法来实现这一点,而不需要为用户构建完整的登录系统。

代码:
下面是从教程中的代码:

数据库迁移:

class CreateComments < ActiveRecord::Migration 
     def change 
     create_table :comments do |t| 
      t.string :commenter 
      t.text :body 
      t.references :article, index: true, foreign_key: true 

      t.timestamps null: false 
     end 
     end 
    end 

控制器:

class CommentsController < ApplicationController 
    def create 
    @article = Article.find(params[:article_id]) 
    @comment = @article.comments.create(comment_params) 
    redirect_to article_path(@article) 
    end 

    private 
    def comment_params 
     params.require(:comment).permit(:commenter, :body) 
    end 
end 

模板:

<p> 
    <strong>Title:</strong> 
    <%= @article.title %> 
</p> 
<p> 
    <strong>Text:</strong> 
    <%= @article.text %> 
</p> 

<h2>Add a comment:</h2> 
<%= form_for([@article, @article.comments.build]) do |f| %> 
    <p> 
    <%= f.label :commenter %><br> 
    <%= f.text_field :commenter %> 
    </p> 
    <p> 
    <%= f.label :body %><br> 
    <%= f.text_area :body %> 
    </p> 
    <p> 
    <%= f.submit %> 
    </p> 
<% end %> 

<%= link_to 'Edit', edit_article_path(@article) %> | 
<%= link_to 'Back', articles_path %> 

删除批注:

class CommentsController < ApplicationController 

  def create 

    @article = Article.find(params[:article_id]) 

    @comment = @article.comments.create(comment_params) 

    redirect_to article_path(@article) 

  end 

  

  def destroy 

    @article = Article.find(params[:article_id]) 

    @comment = @article.comments.find(params[:id]) 

    @comment.destroy 

    redirect_to article_path(@article) 

  end 

  

  private 

    def comment_params 

      params.require(:comment).permit(:commenter, :body) 

    end 

end 

我发现了一个类似的问题here,但没有奏效的答案。

+0

哪里是你破坏方式或删除代码? –

+1

看起来像__代码为me_,我找不到任何努力,您尝试删除用户。 –

+0

@Зелёный我编辑了这个问题,使其更清晰。问题是我不知道从哪里开始。这就是为什么我试图解释我认为我能做什么。所以没有代码尝试。我不想删除一个用户,我只是想让他们删除他们自己的评论。就像这样。如果你写评论,我不应该删除它(就像你不应该删除我的)。 – JustBlossom

回答

2

在CommentsController编辑您的破坏方法如下图所示:

def destroy 
    @article = Article.find(params[:article_id]) 
    @comment = @article.comments.find(params[:id]) 
    if @comment.user.id == current_user.id 
    flash[:success] = "Comment Deleted Successfully!" 
    @comment.destroy 
    else 
    flash[:error] = "You can only delete your own comments!" 
    end 
    redirect_to article_path(@article) 
end 

添加迁移到USER_ID添加到评语表

class AddUserIdToComments < ActiveRecord::Migration 
    def change 
    add_column :comments, :user_id, :integer 
    end 
end 

评论&用户模型应该像下面跟踪用户的评论:

class User < ActiveRecord::Base 
has_many :comments 
end 
class Comment < ActiveRecord::Base 
belongs_to :user 
end 

查看:

<p> 
    <strong>Title:</strong> 
    <%= @article.title %> 
</p> 
<p> 
    <strong>Text:</strong> 
    <%= @article.text %> 
</p> 

<h2>Add a comment:</h2> 
<%= form_for([@article, @article.comments.build]) do |f| %> 
    <p> 
    <%= f.label :commenter %><br> 
    <%= f.text_field :commenter %> 
    <%= f.hidden_field :user_id, current_user.id %> 
    </p> 
    <p> 
    <%= f.label :body %><br> 
    <%= f.text_area :body %> 
    </p> 
    <p> 
    <%= f.submit %> 
    </p> 
<% end %> 

<%= link_to 'Edit', edit_article_path(@article) %> | 
<%= link_to 'Back', articles_path %> 

在评论控制器改变comment_params

def comment_params 
    params.require(:comment).permit(:commenter, :body, :user_id) 
end 
+0

如果评论belongs_to用户,comment.user.id将永远等于comment.user_id – srecnig

+0

@srecnig我已经更新了答案现在检查,这只是一个错误。 –

+0

非常感谢!我试图让它工作,但我得到了以下错误:“在CommentsController中的NoMethodError#销毁未定义的方法'用户'”所以,我试着用@ comment.commenter替换它,但那是不对的。这意味着你最终会遇到同样的问题srecnig提到。 – JustBlossom