首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >向用户和Post模型Rails 4添加注释

向用户和Post模型Rails 4添加注释
EN

Stack Overflow用户
提问于 2015-01-18 15:53:42
回答 2查看 238关注 0票数 0

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

现在我试着让艺术家们对微博发表评论。不过,这不是我想要的结果。这些评论需要同时属于一个特定的艺术家和一个特定的微博。

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

代码语言:javascript
运行
复制
### 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下面

我不能让评论为他们的微博循环。

有什么想法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-01-18 20:16:08

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

代码语言:javascript
运行
复制
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中,您必须为您的评论指定当前登录的艺术家(批注)。

代码语言:javascript
运行
复制
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,请执行如下操作:

代码语言:javascript
运行
复制
class ArtistsController < ApplicationController
  def show
    @artist = Artist.includes(:microposts, :comments => :commenter).find(params[:id])
  end
end
票数 1
EN

Stack Overflow用户

发布于 2015-01-18 18:06:45

满足你的要求。为了建立微型仪器。做这样的事。

代码语言:javascript
运行
复制
artist=Artist who is logged in
artist.artist_micro_posts.build(attributes)
artist.save

在微博上发表评论

代码语言:javascript
运行
复制
micro_posts= Micropost Id
 micro_post.artist_micropost_comments.build(:artist_id=logged in person)
micro_post.save
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28011662

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档