【问题标题】:Add Comment to User and Post models Rails 4向用户和发布模型添加评论 Rails 4
【发布时间】:2015-01-19 04:21:21
【问题描述】:

我有属于艺术家的微博,与之相关的一切都很完美。

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

现在我有一个表单来创建评论,但它只保存在最近的微博 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

我希望它显示艺术家的每个微博,并在每个微博下方显示属于该微博的 cmets。我希望 from 显示在 cmets 下以添加新的 cmets。基本上,我希望它看起来像 Facebook。

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

所以我的两个问题是:

我无法让 cmets 保存在正确的 micropost_id 下

我无法让 cmets 为他们的微博循环。

有什么想法吗?

【问题讨论】:

  • 您能否补充一下Artist、ArtistMicroPost 和ArtistMicropostComment 模型之间的关系?
  • 是的,刚刚添加了。

标签: ruby-on-rails ruby-on-rails-4 activerecord


【解决方案1】:

短名称更易于阅读和理解,因此我将在示例中将您的模型重命名为 Artist、Micropost 和 Comment

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

【讨论】:

    【解决方案2】:

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

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

    用于为微博创建 cmets

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

    【讨论】:

    • 我看不出这和我已经拥有的有什么区别。
    猜你喜欢
    • 2014-08-19
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    • 1970-01-01
    • 2020-05-04
    • 1970-01-01
    • 2010-11-06
    相关资源
    最近更新 更多