简体   繁体   English

Rails 5渲染局部并传递数据

[英]Rails 5 rendering partials and passing data

I'm having trouble getting traction on what the general ways data gets passed around from and is made available to partials. 我很难理解数据从何处传递并可供部分使用的一般方式。

For example: 例如:

I have a controller handing off an instance variable to a template that renders a partial: 我有一个控制器将实例变量传递给呈现局部变量的模板:

static_pages_controller.rb: static_pages_controller.rb:

def home
  @feed_items = current_user.feed
end

home.html.erb: home.html.erb:

<%= render 'shared/feed' %>

_feed.html.erb: _feed.html.erb:

<%= render @feed_items %>

Now, inside my User model is an instance method that reaching into the database to get her posts: 现在,在我的用户模型内部是一个实例方法,该实例方法可以进入数据库以获取其帖子:

user.rb: user.rb:

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

So somehow because Micropost.where(...) returns a collection of microposts is that how Rails knows to go from _feed.html.erb to another partial where the <li> is defined for how microposts want to be defined? 因此以某种方式,因为Micropost.where(...)返回了微_feed.html.erb的集合,是Rails知道如何从_feed.html.erb转到另一部分,其中<li>是如何定义微_feed.html.erb

_micropost.html.erb: _micropost.html.erb:

<li id="micropost-<%= micropost.id %>">
  <%= link_to adorable_avatar_for(micropost.user, size: 50), micropost.user %>
</li>

And also is it just a convention that because I'm really handling a collection of microposts that Rails knows to give the micropost variable? 也是因为我确实在处理Rails知道给micropost变量提供的一系列microposts的约定吗?

Your questions are answered in the Ruby on Rails Guides on Layouts and Rendering . Ruby on Rails布局和渲染指南中回答了您的问题。 It's worth reading the information on partials that comes before the quoted passages below as well: 值得阅读下面引用的段落之前的部分信息:

Every partial also has a local variable with the same name as the partial (minus the underscore). 每个局部变量还具有与局部变量同名的局部变量(减去下划线)。 You can pass an object in to this local variable via the :object option: 您可以通过:object选项将一个对象传递给该局部变量:

 <%= render partial: "customer", object: @new_customer %> 

Within the customer partial, the customer variable will refer to @new_customer from the parent view. 在客户部分中,客户变量将从父视图中引用@new_customer。 (Earlier the Guide instructs that to specify other options for render(), eg object:, you have to explicitly specify partial: and the name of the partial.) (指南早些时候指示要为render()指定其他选项,例如object :,您必须显式指定partial:partial:的名称。)

If you have an instance of a model to render into a partial, you can use a shorthand syntax: 如果您有模型的实例可以呈现为局部模型,则可以使用简写语法:

 <%= render @customer %> 

Assuming that the @customer instance variable contains an instance of the Customer model, this will use _customer.html.erb to render it and will pass the local variable customer into the partial which will refer to the @customer instance variable in the parent view. 假设@customer实例变量包含Customer模型的实例,这将使用_customer.html.erb呈现它,并将局部变量customer传递到在父视图中引用@customer实例变量的局部变量中。

3.4.5 Rendering Collections 3.4.5渲染集合

Partials are very useful in rendering collections. 分部在渲染集合中非常有用。 When you pass a collection to a partial via the :collection option, the partial will be inserted once for each member in the collection: 通过:collection选项将集合传递给局部时,该局部将为集合中的每个成员插入一次:

index.html.erb: index.html.erb:

 <h1>Products</h1> <%= render partial: "product", collection: @products %> 

_product.html.erb: _product.html.erb:

 <p>Product Name: <%= product.name %></p> 

When a partial is called with a pluralized collection, then the individual instances of the partial have access to the member of the collection being rendered via a variable named after the partial. 当用多元集合调用一个局部对象时,该局部对象的各个实例就可以通过以该局部对象命名的变量访问正在呈现的集合成员。 In this case, the partial is _product, and within the _product partial, you can refer to product to get the instance that is being rendered. 在这种情况下,partial是_product,在_product部分中,您可以引用product来获取正在渲染的实例。

There is also a shorthand for this. 这也有一个简写。 Assuming @products is a collection of product instances, you can simply write this in the index.html.erb to produce the same result: 假设@products是产品实例的集合,则只需将其写在index.html.erb中即可产生相同的结果:

 <h1>Products</h1> <%= render @products %> 

Rails determines the name of the partial to use by looking at the model name in the collection. Rails通过查看集合中的模型名称来确定要使用的零件名称。 In fact, you can even create a heterogeneous collection and render it this way, and Rails will choose the proper partial for each member of the collection: 实际上,您甚至可以创建一个异构集合并以这种方式呈现它,Rails会为集合的每个成员选择适当的部分:

index.html.erb: index.html.erb:

 <h1>Contacts</h1> <%= render [customer1, employee1, customer2, employee2] %> 

customers/_customer.html.erb: customer / _customer.html.erb:

 <p>Customer: <%= customer.name %></p> 

employees/_employee.html.erb: employee / _employee.html.erb:

 <p>Employee: <%= employee.name %></p> 

In this case, Rails will use the customer or employee partials as appropriate for each member of the collection. 在这种情况下,Rails将根据集合的每个成员使用客户或雇员的部分信息。

In the event that the collection is empty, render will return nil, so it should be fairly simple to provide alternative content. 如果集合为空,则render将返回nil,因此提供替代内容应该相当简单。

 <h1>Products</h1> <%= render(@products) || "There are no products available." %> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM