在 rails 中,我无法通过每次迭代显示新评论
In rails I can't display the new comment with iteration each do
我在 post show#view 中创建了一个名为 _comments.html.erb
的部分
在这里
<p class="text-center">Poster un commentaire</p>
<%= simple_form_for [post, post.comments.new] do |f| %>
<%= f.error_notification %>
<%= f.input :content, label: "Commentaire"%>
<%= f.submit "Envoyer", class: "btn btn-primary" %>
<% end %>
它会像那样呈现 <%= render 'comments' %>
及以上部分(在 post show#view 中)我做了这样的迭代
<ul class="list-unstyled">
<% @post.comments.each do |comment| %>
<li>
<p><% comment.content %></p>
<% end %>
</li>
</ul>
但是当我创建一条新消息时什么也没有出现,我不明白为什么。
我给你更多的代码细节
post.rb
has_many :comments, dependent: :destroy
comment.rb
belongs_to :user
belongs_to :post
路线是:
resources :posts do
resources :categories
resources :comments
end
评论控制器是
class CommentsController < ApplicationController
before_action :set_post
def create
@comment = @post.comments.build(comment_params)
@comment.user_id = current_user.id
if @comment.save
flash[:success] = "You commented the hell out of that post!"
redirect_to :back
else
flash[:alert] = "There is a problem with your comment"
render root_path
end
end
def destroy
@comment = @post.comments.find(params[:id])
@comment.destroy
flash[:success] = "Comment deleted :("
redirect_to root_path
end
private
def set_post
@post = Post.find(params[:post_id])
end
def comment_params
params.require(:comment).permit(:content, :post_id, :user_id)
end
end
非常感谢您的帮助。
您刚刚在 <p>
行的 erb 模板中错过了 =
。没有它,输出中将不会显示任何内容。另请注意,应交换 </li>
和 end
行,以免将这两个块混合在一起:
<ul class="list-unstyled">
<% @post.comments.each do |comment| %>
<li>
<p><%= comment.content %></p>
</li>
<% end %>
</ul>
顺便说一下,还有另一种渲染集合局部的方法,例如:
<ul class="list-unstyled">
<%= render :partial => "comment", :collection => @comments %>
</ul>
并且不需要遍历 partial 中的值,它将使用 :collection
参数来管理它,然后注释 partial 应该是这样的:
<li>
<p><%= comment.content %></p>
</li>
我在 post show#view 中创建了一个名为 _comments.html.erb
的部分在这里
<p class="text-center">Poster un commentaire</p>
<%= simple_form_for [post, post.comments.new] do |f| %>
<%= f.error_notification %>
<%= f.input :content, label: "Commentaire"%>
<%= f.submit "Envoyer", class: "btn btn-primary" %>
<% end %>
它会像那样呈现 <%= render 'comments' %>
及以上部分(在 post show#view 中)我做了这样的迭代
<ul class="list-unstyled">
<% @post.comments.each do |comment| %>
<li>
<p><% comment.content %></p>
<% end %>
</li>
</ul>
但是当我创建一条新消息时什么也没有出现,我不明白为什么。
我给你更多的代码细节
post.rb
has_many :comments, dependent: :destroy
comment.rb
belongs_to :user
belongs_to :post
路线是:
resources :posts do
resources :categories
resources :comments
end
评论控制器是
class CommentsController < ApplicationController
before_action :set_post
def create
@comment = @post.comments.build(comment_params)
@comment.user_id = current_user.id
if @comment.save
flash[:success] = "You commented the hell out of that post!"
redirect_to :back
else
flash[:alert] = "There is a problem with your comment"
render root_path
end
end
def destroy
@comment = @post.comments.find(params[:id])
@comment.destroy
flash[:success] = "Comment deleted :("
redirect_to root_path
end
private
def set_post
@post = Post.find(params[:post_id])
end
def comment_params
params.require(:comment).permit(:content, :post_id, :user_id)
end
end
非常感谢您的帮助。
您刚刚在 <p>
行的 erb 模板中错过了 =
。没有它,输出中将不会显示任何内容。另请注意,应交换 </li>
和 end
行,以免将这两个块混合在一起:
<ul class="list-unstyled">
<% @post.comments.each do |comment| %>
<li>
<p><%= comment.content %></p>
</li>
<% end %>
</ul>
顺便说一下,还有另一种渲染集合局部的方法,例如:
<ul class="list-unstyled">
<%= render :partial => "comment", :collection => @comments %>
</ul>
并且不需要遍历 partial 中的值,它将使用 :collection
参数来管理它,然后注释 partial 应该是这样的:
<li>
<p><%= comment.content %></p>
</li>