我对Rails很陌生。我完成了一个教程here引导您完成使用它创建博客的步骤。
本教程的一部分将向您展示如何创建一个控制器,该控制器允许用户向文章添加注释。我正在尝试修改它,以便用户只能删除自己的评论(而不是其他人的评论)。
问题:
有没有办法修改代码,这样你就可以限制用户删除他们自己的评论?也欢迎任何资源/教程。我真的不知道该怎么开始。
我觉得正确的方法是在用户提交评论时以某种方式标记用户。将该信息保存在数据库中,然后在有人删除注释时检查该信息。但我想不出一种方法来做到这一点,而不试图为用户建立一个完整的登录系统。
代码
以下是教程中的代码:
数据库迁移:
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
我发现了一个类似的问题,但没有一个有效的答案。
最佳答案
在CommentsControl中,按如下方式编辑销毁方法:
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
添加迁移以将用户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 %>
在注释控制器中更改注释参数
def comment_params
params.require(:comment).permit(:commenter, :body, :user_id)
end
关于ruby-on-rails - 如何限制某人在Rails中删除自己的评论?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36261713/