1

在我的 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%>
4

2 回答 2

2

您可以通过调用user评论上的方法来做到这一点,然后name

<%= comment.user.name %>

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

def to_s
  name
end

这意味着您可以在视图中这样做:

<%= comment.user %>

如果您要加载一大堆评论,那么我建议您以这种方式加载它们:

@comments = Comment.includes(:user)

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

于 2013-07-28T23:48:22.997 回答
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 %>
于 2020-10-01T05:20:32.793 回答