如何显示撰写评论的用户名(Rails)?

时间:2013-07-28 23:28:17

标签: ruby-on-rails blogs

在我的rails应用程序中,我有用户(作者),帖子(文章),评论。如果注册用户在文章上写评论,我想在他的评论旁边显示他的名字,如果他不是注册用户,我想在他的评论旁边显示“匿名”。我怎么能这样做?

评论模型:

class Comment < ActiveRecord::Base
attr_accessible :post_id, :text
belongs_to :post
belongs_to :user
end

用户模型:

class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
has_many :posts, :dependent => :destroy
has_many :comments, :dependent => :destroy

validates :fullname,      :presence => true, :uniqueness => true
validates :password,      :presence => true
validates :email,         :presence => true, :uniqueness => true


devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

attr_accessible :email, :password, :password_confirmation, :fullname


end

发布模型:

class Post < ActiveRecord::Base
attr_accessible :text, :title, :tag_list
acts_as_taggable

validates :user_id, :presence => true
validates :title,   :presence => true
validates :text, :presence => true

belongs_to :user
has_many :comments
end

查看文件(show.html.erb)

<h1><%= @post.title %></h1>

 <p>
 Created: <%= @post.created_at.strftime("%Y/%m/%d")%> by
 <%=  link_to @post.user.fullname, user_posts_path(@post.user) %>
 </p>


<p><%=simple_format @post.text %></p>
<p>
Tags: <%= raw @post.tag_list.map { |t| link_to t, tag_path(t) }.join(', ') %>
</p>

<h2>Comments</h2>
<% @post.comments.each do |comment| %>


<p><%= comment.created_at.strftime("%Y/%m/%d") %>
by <%= HERE I NEED ADD SOMETHING%></p>
<p><%= comment.text %></p>
<p><%= link_to "Delete comment", [@post, comment], :method => :delete, 
:confirm =>"Are   you   sure?"%></p>
<% end %>

<%= form_for [@post, @post.comments.build] do |f| %>
<p><%= f.text_area :text %></p>
<p><%= f.submit "Post comment" %></p>
<% end %>


<% if user_signed_in?%>
<p>
<%= link_to "Back", posts_path %>

<%= link_to "Edit", edit_post_path(@post) %>
<%= link_to "Delete", @post, :method => :delete, :confirm => "Are you sure?"%>
 </p>
<% end%>

2 个答案:

答案 0 :(得分:2)

您可以通过在评论上调用user方法,然后在name上执行此操作:

<%= comment.user.name %>

您还可以在to_s模型中定义User方法:

def to_s
  name
end

这意味着你可以在视图中做到这一点:

<%= comment.user %>

如果您正在加载大量评论,那么我建议您这样加载:

@comments = Comment.includes(:user)

如果那里没有includes(:user),那么Rails会为每个评论发出一个新查询来查找该评论的用户。这样做会使Rails只在一个查询中预先为所有评论加载所有用户。

答案 1 :(得分:0)

我认为您想创建两个指向同一表的关联:

class CreatePostss < ActiveRecord::Migration
  def change
    create_table :posts do |t|
      t.text :text
      t.references :user, index: true, foreign_key: true
      t.references :author, index: true, foreign_key: { to_table: :users }
      t.timestamps null: false
    end
  end
end

此处user_id引用帖子的目标用户,author_id引用撰写帖子的用户。两者都引用了users.id。

然后在您的帖子模型中创建两个belongs_to关联:

class Post < ApplicationRecord
  belongs_to :user
  belongs_to :author, 
    class_name: 'User', 
    inverse_of: :authored_posts
end

您的用户模型中还有两个has_many关联:

class User < ApplicationRecord
  # posts about this user
  has_many :posts 
  # postss written by this user 
  has_many :authored_posts, 
    class_name: 'Post',
    foreign_key: :author_id,
    inverse_of: :author
end

这是控制者

class PostsController < ApplicationController
  before_action :set_user, only: [:new, :create]
  
  # ...

  def new
    @post = @user.posts.new
  end

  def create
    @post = @user.posts.new(post_params) do |c|
      c.author = current_user
    end
    if @post.save
      redirect_to doctor_path(id: @user.id)
    else
      render :new
    end
  end 

  private

  def set_user
    @user = User.find(params[:id])
  end

  # ...
end

要显示帖子,您将执行以下操作:

<% @user.posts.each do |post| %>
<div class="post">
  <div class="body">
    <%= post.body %>
  </div>
  <p class="author"><%= post.author.email %></p>
</div>  
<% end %>