Rails如何呈现条件局部

时间:2011-06-25 02:13:18

标签: ruby-on-rails ruby-on-rails-3

现在我有这样的事情:

<%= render :partial => "widgets/type1.html.erb", :locals => {:widget => @wall} %>

我有50种不同类型

widgets/type1.html.erb
widgets/type2.html.erb
widgets/type3.html.erb
widgets/type4.html.erb
....

在widget对象中,有一个widget.type,它有type2等...有没有办法让部分动态来避免所有的if语句?

由于

1 个答案:

答案 0 :(得分:3)

由于你可以告诉rails渲染:partial =&gt; string,你可以在该字符串中进行字符串替换。所以,如果@widget是变量名:

<%= render :partial => "widgets/#{@widget.type}", :locals => {:widget => @wall} %>