Rails:渲染模型? [英] Rails: Rendering Models?
问题描述
我可以想到一百万种在 Rails 中渲染模型的非自动方法,但我想知道是否有一些内置方法可以做到这一点.我希望能够做到这一点
I can think of a million not-so-automatic ways to render a model in Rails, but I'm wondering if there's some built-in way to do it. I'd like to be able to this
<%=@thing -%>
很明显,你可以用局部来做到这一点(我的意思是,调用 render :partial),但我想知道是否有一些标准的方法可以将视图与模型关联.
obviously with partials you can do it (I mean, calling render :partial), but I'm wondering if there's some standard way to associate views with models.
[提前感谢,weppos,修复这个问题的标签:)]
[Thanks in advance, weppos, for fixing the tags on this question :)]
推荐答案
如果直接将模型传递给 render 它将尝试为其渲染部分.
If you pass a model directly to render it will attempt to render a partial for it.
<%= render @thing %>
那是一样的.
<%= render :partial => 'things/thing', :object => @thing %>
如果你传递一个模型数组...
If you pass an array of models...
<%= render @things %>
它将为每个渲染 _thing
部分,就像你这样做一样.
It will render the _thing
partial for each as if you did this.
<%= render :partial => 'things/thing', :collection => @things %>
注意:这需要 Rails 2.3.如果您有早期版本的 Rails,则需要使用 :partial 选项来做同样的事情.
Note: this requires Rails 2.3. If you have earlier versions of Rails you'll need to use the :partial option to do the same thing.
<%= render :partial => @thing %>
这篇关于Rails:渲染模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!