Rails: rendering models?

I can recall a million manual ways to render a model in Rails, but I wonder if there is a built-in way to do this. I would like to be able to do this

<% =@thing -%> 

obviously, with partial, you can do this (I mean by calling render: partial), but I'm wondering if there is a standard way to map views to models.

[Thanks in advance, weppos, for fixing tags on this :) :)]

+4
source share
3 answers

If you pass the model directly to render , it will try to make it partial for it.

 <%= render @thing %> 

The same as.

 <%= render :partial => 'things/thing', :object => @thing %> 

If you pass an array of models ...

 <%= render @things %> 

It will be partial to _thing for everyone, as if you did it.

 <%= render :partial => 'things/thing', :collection => @things %> 

Note: this requires Rails 2.3. If you have earlier versions of Rails, you need to use the: partial option to do the same.

 <%= render :partial => @thing %> 
+13
source

You can override the to_s method in your model to return the desired view, although this is not necessarily desirable, because then your model contains presentation problems that are relevant to your view.

Furthermore, to_s really intended to return a short string representation of your model, useful for debugging purposes, etc.

+1
source

You did not come from Seaside . :) (I ask, because this is exactly how everything works where each model / renderable object knows how to make itself, and this is how you lay out the page.)

As for your actual question, the standard way to do this is to make it partial that you feed your @thing. (i.e. you are right for partial money, and this is how submissions are usually associated with models.)

+1
source

All Articles