我有属于艺术家的微博,而且所有的东西都完美无缺。
现在我想让艺术家评论一下微博。但是,这不符合我的要求。评论需要属于特定的艺术家和特定的微博。
现在我有一个表单来创建评论,但它只保存在最新的微博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
我希望艺术家在每个微博下面显示每个微博,以显示属于微博的评论。我希望from在评论下显示添加新评论。基本上,我希望它看起来像Facebook。
现在,所有评论都显示在每个微博下,无论micropost_id和create方法在任何micropost_id下都不会创建,除了最近的微博。
所以我的两个问题是:
我无法在正确的micropost_id下保存评论
我无法让评论循环播放他们的微博。
有什么想法吗?
答案 0 :(得分: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
答案 1 :(得分:0)
根据您的要求。用于创建微博。 做这样的事情。
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