0
我是Rails中的新成员,在我从他们的(tutorial)创建Article模型后,我想使用Comment模型,但它由两部分组成:第一个是“Commenter”输入将出现的名称除了评论和评论的“主体”之外。如何使用current_user自动分配评论者?
由于我使用设计,我想跳过评论者输入,因此用户只需输入他/她的评论,并且他们的用户名将自动分配给评论。我有一切设置(设计,评论模型,用户模型等),并将用户名字段集成到设计宝石,以便它可以与current_user.username一起使用。我使用Rails 4
这是_comment.html.erb的代码
<p>
<strong>Commenter:</strong>
<%= comment.commenter %>
</p>
<p>
<strong>Comment:</strong>
<%= comment.body %>
</p>
<p>
<%= link_to 'Destroy Comment', [comment.article, comment],
method: :delete,
data: { confirm: 'Are you sure?' } %>
</p>
评论控制器:
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
完全做了这个把戏而不是“commenter_id”和“commenter_name”,因为它只是一个字符串变量,你只需要合并“commenter:current_user.username”谢谢 – Lautaro 2015-03-03 01:27:02