向用户和 Post 模型添加评论 Rails 4

Add Comment to User and Post models Rails 4

我有属于艺术家的微博,一切都完美无缺。

现在我正在尝试让艺人评论微博。但是,这不是我想要的。评论需要属于特定的艺术家和特定的微博。

现在我有一个创建评论的表单,但它只保存在最近的微博ID下。

### controllers/artists/comments_controller.rb ###

class Artists::CommentsController < ApplicationController

  def create
    @micropost = ArtistMicropost.find_by(params[:micropost_id])
    @comment = @micropost.artist_micropost_comments.build(comment_params)
    @comment.artist_id = current_artist.id
  end

    private

      def comment_params
        params.require(:artist_micropost_comment).permit(:artist_micropost_id, :artist_id, :content)
      end

end

### controllers/artists/artists_controller.rb ###

class Artists::ArtistsController < ApplicationController

  def show
    @artist = Artist.find(params[:id])
    @micropost = ArtistMicropost.new
    @micro = ArtistMicropost.find_by(params[:micropost_id])
    @comment = ArtistMicropostComment.new
  end

end

### views/artists/show.html.erb ###

<% @artist.artist_microposts.each do |micropost| %>
  ...
  <%= micropost.content %>
  ...

  <% @micro.artist_micropost_comments.each do |comment| %>
    <%= comment.content %>
  <% end %>

  <%= form_for(@comment) do |f| %>
    <%= f.text_area :content %>
    <%= f.submit "post comment" %>
  <% end %>

<% end %>

### models/artist.rb ###

class Artist < ActiveRecord::Base
  has_many :artist_microposts, dependent: :destroy
  has_many :artist_micropost_comments, dependent: :destroy
end

### models/artist_micropost.rb ###

class ArtistMicropost < ActiveRecord::Base
  belongs_to :artist
  has_many :artist_micropost_comments, dependent: :destroy
end

### models/artist_micropost_comment.rb ###

class ArtistMicropostComment < ActiveRecord::Base
  belongs_to :artist_micropost
  belongs_to :artist
end

我想让它显示艺术家的每条微博,每条微博下面显示属于该微博的评论。我希望从显示在评论下以添加新评论。基本上,我希望它看起来像 Facebook。

现在,无论micropost_id是什么,每条微博下都会显示所有评论,create方法不会在任何micropost_id下创建,除了最近的一条。

所以我的两个问题是:

我无法将评论保存在正确的 micropost_id

我无法让评论循环播放他们的微博。

有什么想法吗?

满足您的要求。用于创建微博。 做这样的事情。

artist=Artist who is logged in
artist.artist_micro_posts.build(attributes)
artist.save

用于创建微博评论

micro_posts= Micropost Id
 micro_post.artist_micropost_comments.build(:artist_id=logged in person)
micro_post.save

短名称更易于阅读和理解,因此我会将示例中的模型重命名为 ArtistMicropostComment

class Artist < ActiveRecord::Base
  has_many :microposts, dependent: :destroy
  has_many :comments, through: :microposts, dependent: :destroy
end

class Micropost < ActiveRecord::Base
  belongs_to :artist
  has_many :comments, dependent: :destroy
end

class Comment < ActiveRecord::Base
  # I renamed artist to commenter to make it clear that is not the same artist as the one that created the micropost,
  # this implies that instead of author_id you will have commented_id in comments table

  belongs_to :commenter, :class_name => Artist
  belongs_to :micropost
end


### views/artists/show.html.erb ###

<% @artist.microposts.each do |micropost| %>
  ...
  <%= micropost.content %>
  ...


  <% micropost.comments.each do |comment| %>
    # here you display comments for each micropost
    <%= comment.content %>

    # pay attention at the way I builded the comment
    <%= form_for(micropost.comments.build) do |f| %>
      <%= f.hidden_field :micropost_id %> # this will make the link to your micropost
      <%= f.text_area :content %>
      <%= f.submit "post comment" %>
    <% end %>
  <% end %>
<% end %>

在您的 comments_controller 中,您必须将当前登录的艺术家(评论者)分配给您的评论。

class CommentsController < ApplicationController
  def create
    @comment = Comment.new(comment_params)
    @comment.commenter = current_artist

    if @comment.save
      ...
    end 
  end

  private

  def comment_params
    params.require(:comment).permit(:micropost_id, :content)
  end
end

为了避免在加载艺术家、微博和评论时出现 N+1,请执行以下操作:

class ArtistsController < ApplicationController
  def show
    @artist = Artist.includes(:microposts, :comments => :commenter).find(params[:id])
  end
end