【问题标题】:instance vs local variables in a partial局部变量中的实例与局部变量
【发布时间】:2012-07-11 23:15:55
【问题描述】:

我目前正在阅读 Michael Hartl 的教程 Ruby on Rails 教程 http://ruby.railstutorial.org/ruby-on-rails-tutorial-book。我对某些部分变量的来源感到困惑。在他的教程中,他创建了用户和微博。用户可以在他的主页上创建一个 Micropost(称为 Feed)并将它们发布到那里。布局看起来像这样http://ruby.railstutorial.org/chapters/user-microposts#fig:proto_feed_mockup。现在用户模型看起来像这样(我没有发布整个内容):

class User < ActiveRecord::Base
  has_many :microposts, dependent: :destroy

  def feed
    Micropost.where("user_id = ?", id)
  end
end

Micropost 模型如下所示:

class Micropost < ActiveRecord::Base
  belongs_to :user
end

作者在文中说 User 模型中的 feed 方法可以等效地写成这样:

def feed
  microposts
end

为什么它们是一样的?

我的下一个问题与部分有关。如果我没记错的话,在用户的显示页面 (show.html.erb) 上会调用 _microposts.html.erb:

<%= render @microposts %>

_microposts.html.erb 看起来像这样:

<li>
  <span class="content"><%= micropost.content %></span>
  <span class="timestamp">
    Posted <%= time_ago_in_words(micropost.created_at) %> ago.
  </span>
  <% if current_user?(micropost.user) %>
    <%= link_to "delete", micropost, method: :delete,
      data: { confirm: "You sure?" },
      title: micropost.content %>
  <% end %>
</li>

我的问题是 micropost 变量来自哪里?和调用这个部分的@micropost 变量一样吗?

现在在用户主页 (home.html.erb) 上有一个对 _feed.html.erb 部分的调用,如下所示:

<%= render 'shared/feed' %>

_feed.html.erb 看起来像这样:

<% if @feed_items.any? %>
  <ol class="microposts">
    <%= render partial: 'shared/feed_item', collection: @feed_items %>
  </ol>
  <%= will_paginate @feed_items %>
<% end %>    

我知道@feed_items 来自哪里。它设置在控制器中。现在 _feed_item.html.erb 看起来像这样:

<li id="<%= feed_item.id %>">
  <%= link_to gravatar_for(feed_item.user), feed_item.user %>
  <span class="user">
    <%= link_to feed_item.user.name, feed_item.user %>
  </span>
  <span class="content"><%= feed_item.content %></span>
  <span class="timestamp">
    Posted <%= time_ago_in_words(feed_item.created_at) %> ago.
  </span>
  <% if current_user?(feed_item.user) %>
    <%= link_to "delete", feed_item, method: :delete,
      data: { confirm: "You sure?" },
      title: feed_item.content %>
  <% end %>
</li>

所以一个类似的问题是变量 feed_item 来自哪里,它包含什么?

谢谢, 迈克

【问题讨论】:

    标签: ruby ruby-on-rails-3


    【解决方案1】:

    好的,让我们看看。一口气问了很多问题,但是...

    1. 为什么 'feed' 等同于 'microposts'? 这是 Rails 的协会在工作。当您使用has_many 描述关联时,Rails 会根据关联名称创建一大堆方法。在这种情况下,您说用户 has_many :microposts,其中包括创建 User#microposts 方法。

    2. 渲染调用中使用的实例变量 (@microposts) 可能是在控制器操作中设置的。当您以这种方式(使用 ActiveRecord 对象数组)调用 render 时,Rails 会查找名称与这些对象的类名匹配的部分。在本例中,它们是 MicroPost 对象,因此它会查找名为 _micropost 的部分对象,并为数组中的每个对象呈现一次。在渲染局部时,可以使用与局部同名的局部变量来引用与局部关联的对象。由于这是_micropost 部分,本地micropost 变量指的是它正在呈现的对象。

    3. 再一次,与局部同名的局部变量指的是局部正在渲染的对象。 @feed_items 是一个集合,对于其中的每个对象,您都会获得 _feed_item 部分的渲染,其中 feed_item 局部变量引用该对象。

    【讨论】:

      【解决方案2】:
      1. 因为用户的微博是使用has_many关联的,而在内部,关系是基于用户的id。 “手动”获取它们本质上是相同的,但需要更多的工作。
      2. micropost 来自约定——Rails 为您创建它。我不知道您所说的“调用此部分的 @micropost 变量”是什么意思。
      3. 相同的答案,尽管它明确基于模板名称 (IIRC) 而不是单数化名称。它包含 @feed_items 包含的任何一个。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多