我的主题与this问题非常相似,只是我的评论显示在bootstrap模式中。我需要创建一个注释部分,以便我可以使用respond_to |format| do
format.js
呈现部分并更新模态中的注释。
问题#1。如何创建评论部分?这是代码:
CommentsController
def create
@photo = Photo.find(params[:photo_id])
@comment = @photo.comments.build(comment_params)
@comment.save
respond_to do |format|
format.html { redirect_to :back }
format.js
end
end
UsersController
def show
@user = User.find(params[:id])
@photos = @user.photos.order('created_at desc').paginate(page: params[:page], per_page: 12)
end
Users/show.html.erb
<% @photos.in_groups_of(3, false).each do |group| %>
<div class="row instagram">
<% group.each do |photo| %>
<a data-toggle="modal" href=<%="#"+"#{photo.id}"%>>
<%= image_tag(photo.picture.ad.url, class: "img-responsive")%>
</a>
<div class="modal" id=<%="#{photo.id}"%> tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
.
.
<div class="col-sm-4">
<div class="instacomments">
<% photo.comments.each do |comment| %>
<p class="commentblock">
<%= link_to comment.user.name, user_path(comment.user_id)%><span> </span><%= comment.content %>
</p>
<% end %>
</div>
</div>
答案 0 :(得分:1)
在show.html.erb
:
<%= render partial: "comments/comment", collection: photo.comments, as: :comment %>
请注意,as: :comment
键是为视图中的每个集合项设置局部变量的名称。
现在您可以在comment
中引用comments/_comment.html.erb
变量:
<p class="commentblock">
<%= link_to comment.user.name, user_path(comment.user_id)%>
<span> </span> <!-- Not sure why you have an empty <span> here -->
<%= comment.content %>
</p>