I'm iterating over a model object @products (currently 3 in the DB), but I also need to apply different styling to each of the objects returned in my view.
我正在迭代一个model object @products(目前在DB中为3),但是我还需要对视图中返回的每个对象应用不同的样式。
Here is my current code in my page_controller.rb to get only the first 3 products:
这是我的page_controller中的当前代码。rb只获得前3个产品:
@products = Product.where(id: 1..3)
In my view index.html.erb:
在我看来index.html.erb:
<div class="row pricing-table">
<% @products.each do |p| %>
<div class="col-md-4">
<div class="panel <%= custom-style %>">
<div class="panel-heading"><h3 class="text-center"><%= p.title %></h3></div>
<div class="panel-body text-center">
<p class="lead" style="font-size:40px"><strong><%= number_to_currency(p.price, precision: 0) %>/month</strong></p>
</div>
<ul class="list-group list-group-flush text-center list-no-bullets">
<%= p.description.html_safe %>
</ul>
<div class="panel-footer">
<%= link_to("Request Quote", "/quote_forms/new", class: "btn btn-lg btn-block btn-danger-override") %>
</div>
</div>
</div>
<% end %>
</div>
Now, what I'm trying to do is add a custom style to the 4th line div
element. (I've put in erb syntax so you can see where I'm talking about)
现在,我要做的是向第4行div元素添加自定义样式。(我加入了erb语法,这样你们就能明白我在说什么)
So, I added an array of the CSS style classes to page_controller.rb:
因此,我向page_controller.rb添加了CSS样式类的数组:
@style = ["danger", "info", "success"]
In hopes that there is a way to get the 4th line div
element to be:
希望有一种方法可以使第4行div元素为:
<div class="panel panel-danger">
in the first iteration, then in the second iteration:
在第一个迭代中,然后在第二个迭代中:
<div class="panel panel-info">
and so on.
等等。
How would I be able to do this?
我要怎么做呢?
1 个解决方案
#1
3
Use each_with_index
to iterate over products and then use the index to get the corresponding style name:
使用each_with_index对产品进行迭代,然后使用索引获得相应的样式名:
<div class="row pricing-table">
<% @products.each_with_index do |p, p_index| %>
<div class="col-md-4">
<div class='panel <%= "panel-#{@style[p_index % @style.size]}" %>'>
<div class="panel-heading"><h3 class="text-center"><%= p.title %></h3></div>
<div class="panel-body text-center">
<p class="lead" style="font-size:40px"><strong><%= number_to_currency(p.price, precision: 0) %>/month</strong></p>
</div>
<ul class="list-group list-group-flush text-center list-no-bullets">
<%= p.description.html_safe %>
</ul>
<div class="panel-footer">
<%= link_to("Request Quote", "/quote_forms/new", class: "btn btn-lg btn-block btn-danger-override") %>
</div>
</div>
</div>
<% end %>
</div>
#1
3
Use each_with_index
to iterate over products and then use the index to get the corresponding style name:
使用each_with_index对产品进行迭代,然后使用索引获得相应的样式名:
<div class="row pricing-table">
<% @products.each_with_index do |p, p_index| %>
<div class="col-md-4">
<div class='panel <%= "panel-#{@style[p_index % @style.size]}" %>'>
<div class="panel-heading"><h3 class="text-center"><%= p.title %></h3></div>
<div class="panel-body text-center">
<p class="lead" style="font-size:40px"><strong><%= number_to_currency(p.price, precision: 0) %>/month</strong></p>
</div>
<ul class="list-group list-group-flush text-center list-no-bullets">
<%= p.description.html_safe %>
</ul>
<div class="panel-footer">
<%= link_to("Request Quote", "/quote_forms/new", class: "btn btn-lg btn-block btn-danger-override") %>
</div>
</div>
</div>
<% end %>
</div>