简体   繁体   English

Rails通过local_assigns部分循环

[英]Rails partial looping through local_assigns

I'm populating a partial with many instance variables in the locals hash. 我在局部变量中填充了很多实例变量的局部变量。 Is there a way to pull multiple instance variables from local_assigns in the partial? 有没有办法从局部中的local_assigns中提取多个实例变量?

<%= render 'tabs', :locals => {:tab1 => @news, :tab2 => @people, :tab3 => @tags} %>

In the partial I'd like to create a dynamic set of tabs based on the number of instance variables I populate the locals with. 在部分文档中,我想根据填充本地变量的实例变量的数量来创建动态选项卡集。

<% local_assigns.each do |local_assign| %>
  <% local_assign.each do |tabs| %>
     <!-- Grab tab class name -->
     <li class="tab"><%= tabs[:class] %></li>
  <% end %>
<% end %>

Why don't you just pass it as an array 为什么不将其作为数组传递

render 'tabs', :locals => {:tabs => [@news, @people, @tags]} %>

in your view 在你看来

<% tabs.each do |tab| %>
   <!-- Grab tab class name -->
   <li class="tab"><%= tab[:class] %></li>
<% end %>

Thanks Vimsha I ended up using collection instead of locals and created the instance variable in the controller. 感谢Vimsha,我最终使用collection而不是locals并在控制器中创建了实例变量。

http://guides.rubyonrails.org/layouts_and_rendering.html http://guides.rubyonrails.org/layouts_and_rendering.html

Controller
@tabs = [@news, @people]

View
<%= render 'tabs', :collection => @tabs %> 

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

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