现在,所有评论下显示的用户名都来自当前登录的用户。它不显示实际发表评论的正确用户。我之前遇到过这个问题,我想我已经解决了。我是一个Rails新手所以请轻松一下。如果需要更多代码段,请告诉我。先感谢您。
查看/ _comment
<%= div_for(comment) do %>
<p>
<strong>Posted <%= time_ago_in_words(comment.created_at) %> ago</strong></br>
<%= h(comment.body) %>
<strong><%= current_user.name %></strong>
<%= link_to 'Delete', [@post, comment], :method => :delete, :confirm => "Are you sure?" %>
</p>
<% end %>
查看/ _form
<%= form_for [@post, @comment] do |f| %>
<% if @comment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@comment.errors.count, "error") %> prohibited this comment from being saved:</h2>
<ul>
<% @comment.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit class: "btn btn-danger btn-sm" %>
</div>
<% end %>
评论控制器
class CommentsController < ApplicationController
before_filter :authenticate_user!
def new
@post = Post.find(params[:post_id])
@comment = Comment.new
end
def create
@post = Post.find_by_id(params[:post_id])
@comment = @post.comments.create(params.require(:comment).permit(:commenter, :body))
@comment.username = current_user
respond_to do |format|
if @comment.save
format.html { redirect_to @post, notice: 'Comment was successfully created.' }
format.json { render json: @comment, status: :created, location: @comment }
else
format.html { render action: "new" }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
def destroy
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
@comment.destroy
redirect_to post_path(@post)
end
def show
@commment = Comment.find params[:id]
end
型号/评论
class Comment < ActiveRecord::Base
belongs_to :post
belongs_to :user
end
移植
class CreateComments < ActiveRecord::Migration
drop_table :comments
def change
create_table :comments do |t|
t.integer :post_id
t.text :body
t.timestamps
end
add_index :comments, :post_id
end
end
答案 0 :(得分:0)
我看到你的评论对象有一个username
属性(来自create
方法)?:
@comment.username = current_user
如果有,请将current_user.name
更改为comment.username
:
<%= div_for(comment) do %>
<p>
<strong>Posted <%= time_ago_in_words(comment.created_at) %> ago</strong></br>
<%= h(comment.body) %>
<strong><%= comment.username %></strong>
<%= link_to 'Delete', [@post, comment], :method => :delete, :confirm => "Are you sure?" %>
</p>
<% end %>
编辑28/08/2014
如果您喜欢它以便您必须是系统中的用户才能发表评论,那么您可以实现以下关系:
class User < ActiveRecord::Base
# they can create posts
has_many :posts
# they can add comments to posts
has_many :comments
# they have comments directed back at them through the posts they create
has_many :responses, :through => :posts, :source => :comments
end
class Post < ActiveRecord::Base
# each post is created by a user
belongs_to :user
# a post has comments
has_many :comments
end
class Comment < ActiveRecord::Base
# each comment is attached to a post
belongs_to :post
# each comment is created by a user
belongs_to :user
end
此关联将允许您关联代理,如:
@user.comments
- 返回用户发表的评论,无论帖子@user.responses
- 返回附加到用户发布的帖子的评论@comment.user
- 返回发布评论的用户的用户对象@comment.post
- 返回评论所属的帖子@post.comments
- 返回附在帖子上的所有评论使用此功能,您可以使用@comment.user_id = current_user.id
中的CommentsController
将评论与用户相关联。然后在您的视图中,代替<%= current_user.name %>
,您可以放置<%= comment.user.name %>
(用name
代替您调用的属性。)
记住 :这种方式假设您必须是系统中的用户才能发表评论。如果评论者是否是您系统中的用户并不重要......
评论模型需要保留制作它的人的名字。在迁移中,Comment模型仅创建:body
和:post_id
个表(timestamps
按惯例自动生成):
create_table :comments do |t|
t.integer :post_id
t.text :body
t.timestamps
end
您已经告诉CommentsController
接受create
方法中的两个参数:
@comment = @post.comments.create(params.require(:comment).permit(:commenter, :body))
所以,你不需要创建另一个属性(&#34;用户名&#34;);您只需将commenter
属性添加到评论模型即可!首先运行rake db:migrate
以确保您没有任何未完成的迁移,然后生成迁移以添加&#34;评论者&#34;列到您的&#34;评论&#34;表:
rails generate migration add_commenter_to_comments commenter:string
之后,像往常一样运行rake db:migrate
以运行新迁移。完成后,您将能够在控制器中使用@comment.commenter
,并感谢一些ActiveRecord魔法。确保在评论表单中添加一个位置,以便人员添加他们的名字!
P.S。你偶然读到 Beginning Rails (4) 吗?这段代码看起来非常熟悉......
P.P.S。我也是一个Rails n00b。我和你一起学习......:)
答案 1 :(得分:0)
我看到您将用户对象(在本例中为current_user)分配到@ comment.username = current_user
@comment.username = current_user
您不应该以这种方式将整个用户对象传递给它,它应该是来自用户对象的适当字段; e.g。
@comment.username = current_user.username
以下是一些要澄清的内容:
干杯!