Ruby on rails 在rails上声明一个变量ruby

Ruby on rails 在rails上声明一个变量ruby,ruby-on-rails,variables,declare,Ruby On Rails,Variables,Declare,在中给出的示例中 我被变量“comment”弄糊涂了 原文是 <h2>Comments</h2> <% @post.comments.each do |comment| %> <p> <strong>Commenter:</strong> <%= comment.commenter %> </p> <p> <strong>Comment:

在中给出的示例中

我被变量“comment”弄糊涂了

原文是

<h2>Comments</h2>
<% @post.comments.each do |comment| %>
  <p>
    <strong>Commenter:</strong>
    <%= comment.commenter %>
  </p>

  <p>
    <strong>Comment:</strong>
    <%= comment.body %>
  </p>
<% end %>
注释

评论者:

评论:

在这个版本中,我可以理解“评论”来自于|评论|

然后,在第7节中,该部分改为

<h2>Comments</h2>
<%= render @post.comments %>
注释
在app/views/comments/_comment.html.erb中 代码是

<p>
  <strong>Commenter:</strong>
  <%= comment.commenter %>
</p>

<p>
  <strong>Comment:</strong>
  <%= comment.body %>
</p>

评论者:

评论:

然后我就糊涂了。“注释”在哪里声明

有没有介绍如何在RubyonRails中声明变量的教程

非常感谢。

应该清理一下。它的核心是

<% render @post.comments %>

将做与相同的事情

<% @post.comments.each do |comment| %>
  <%= render partial: 'comments/comment', locals: { comment: comment } %>
<% end %>


部分模板中的变量
comment
render
声明,并由
locals
散列触发。这篇文章进一步解释了如何从前者获得后者的魔力。

谢谢。我感到非常困惑的地方是“评论”在哪里。我们有“评论”。但是我没有从“评论”到“评论”的规则。如果我有,我应该一直期待“做abc”?顺便问一下,你能告诉我这个魔术的解释链接吗?我找不到它?@user3440433,是链接,你没有收到吗?是的,
render@post.abcs
将在你的部分模板中定义一个
abc
本地。