Multivariable Partial in Ruby on Rails

 ̄綄美尐妖づ 提交于 2019-12-24 19:52:49

问题


I have a partial that I want rendered with a collection and another variable. Is it possible to pass more than one variable to a partial?

To illustrate:

Category HABTM Brands

This is just semi-pseudo-code, but I want to do something like:

<% @categories.each do |c| %>
    <%= c.name %>
    <%= render :partial => "mypartial", :collection => c.brands, :object => c.id %>
<% end %>

The partial needs the category id as well as the "current_brand". Any ideas?


回答1:


Inside of your view, you pass a hash to the :locals key-value pair in the options hash argument.

<%= render :partial => 'partial', :locals => { :foo => 'a', :bar => 'b' } %>

... and these keys become available as variables in your partials.

Foo is: <%= foo %>

Bar is: <%= bar %>



回答2:


You can give a partial any number of variables with the :locals option. It takes a hash of variable names and values.



来源:https://stackoverflow.com/questions/1186504/multivariable-partial-in-ruby-on-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!