多变量部分在Ruby on Rails [英] Multivariable Partial in Ruby on Rails

查看:183
本文介绍了多变量部分在Ruby on Rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个部分,我想用一个集合和另一个变量呈现。

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?

说明:

类别HABTM品牌

这只是半伪代码,但我想做一些类似的操作:

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 %>

部分需要类别ID以及current_brand。任何想法?

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

推荐答案

在你的视图内,你传递一个哈希到:locals 键值对在选项哈希参数中。

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 %>

这篇关于多变量部分在Ruby on Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆