2016-03-28 29 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