问题描述
我无法理解数据从哪些一般方式传递过来并提供给部分数据的方式.
I'm having trouble getting traction on what the general ways data gets passed around from and is made available to partials.
例如:
我有一个控制器将实例变量传递给呈现部分的模板:
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 %>
现在,在我的 User 模型中有一个实例方法,可以访问数据库以获取她的帖子:
Now, inside my User model is an instance method that reaching into the database to get her posts:
user.rb:
def feed
Micropost.where("user_id = ?", id)
end
不知何故,因为 Micropost.where(...)
返回一组微博,Rails 知道如何从 _feed.html.erb
转到另一个部分 where 是为微博的定义方式定义的?
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.html.erb:
_micropost.html.erb:
<li id="micropost-<%= micropost.id %>">
<%= link_to adorable_avatar_for(micropost.user, size: 50), micropost.user %>
</li>
这是否只是一个约定,因为我确实在处理一组 microposts
,Rails 知道给 micropost
变量赋值?
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?
推荐答案
您的问题在 Ruby on Rails 布局和渲染指南.在下面引用的段落之前的部分信息也值得一读:
Your questions are answered in the Ruby on Rails Guides on Layouts and Rendering. It's worth reading the information on partials that comes before the quoted passages below as well:
每个部分也有一个与局部变量同名的局部变量部分(减去下划线).你可以将一个对象传递给这个通过 :object 选项的局部变量:
<%= render partial: "customer", object: @new_customer %>
在客户部分中,客户变量将引用@new_customer 来自父视图.(之前指南指示为 render() 指定其他选项,例如 object:,您必须明确指定 partial:
和部分的名称.)
Within the customer partial, the customer variable will refer to @new_customer from the parent view. (Earlier the Guide instructs that to specify other options for render(), e.g. object:, you have to explicitly specify partial:
and the name of the partial.)
如果你有一个模型的实例要渲染成局部,你可以使用速记语法:
If you have an instance of a model to render into a partial, you can use a shorthand syntax:
<%= render @customer %>
假设@customer 实例变量包含一个实例Customer 模型,这将使用 _customer.html.erb 来呈现它和将局部变量 customer 传递给部分,这将引用父视图中的@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.
3.4.5 渲染集合
局部在渲染集合中非常有用.当你通过一个通过 :collection 选项收集到部分,部分将为集合中的每个成员插入一次:
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:
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>
当使用复数集合调用部分时,则部分的各个实例可以访问该部分的成员集合通过以部分命名的变量呈现.在在这种情况下,部分是 _product,在 _product 部分中,您可以参考 product 获取正在渲染的实例.
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.
这也有一个简写.假设@products 是一个集合产品实例,您可以简单地将其写入 index.html.erb产生相同的结果:
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:
<h1>Products</h1>
<%= render @products %>
Rails 通过查看部分来确定要使用的部分名称集合中的模型名称.事实上,你甚至可以创建一个异构集合并以这种方式呈现,Rails 会选择集合中每个成员的正确部分:
Rails determines the name of the partial to use by looking at the model name in the collection. 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:
index.html.erb:
index.html.erb:
<h1>Contacts</h1>
<%= render [customer1, employee1, customer2, employee2] %>
customers/_customer.html.erb:
customers/_customer.html.erb:
<p>Customer: <%= customer.name %></p>
employees/_employee.html.erb:
employees/_employee.html.erb:
<p>Employee: <%= employee.name %></p>
在这种情况下,Rails 将使用客户或员工部分作为适合集合中的每个成员.
In this case, Rails will use the customer or employee partials as appropriate for each member of the collection.
如果集合为空,render会返回nil,所以提供替代内容应该相当简单.
In the event that the collection is empty, render will return nil, so it should be fairly simple to provide alternative content.
<h1>Products</h1>
<%= render(@products) || "There are no products available." %>
这篇关于Rails 5 渲染部分和传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!