Dale C. wrote:
Cna anyone point e at any rails 2.0 tutorials on simple use of partials
for forms in new and update CRUD operations?
At a basic level, you can think of a partial as a Lego building block…
from which you create your web form. You can have any number of
partials, each of which presents a different piece of your final
I use partials for all sorts of things on a web form - menus, content,
navigation links, etc.
Let’s say, from your example, that you want the new action for some
model to show ALL the attributes of the model, but you want the edit
action to only show a subset of the model attributes. If you put the
code related to the attributes common to those actions (new and edit)
into a partial, the new.html.erb can show the fields it needs to show
AND render the partial while the edit.html.erb would just render the
The value of partials really comes weighing the reusability of a chunk
of code and the cost to execute that hunk of code…
Would we put something into a partial that’s used only one place?
But, if your app shows, for instance, an address on multiple different
forms, you can create a partial that renders an address, and all the
places that need to show an address can employ that same partial. Less
code for you to maintain, and an enforced consistency within your
application - all addresses render the same. If it’s a duck, it should
look like a duck.
The next extension to that idea, at least for me, was to only render
something again (invoke the partial) if it changed since the last time
it was rendered. This gets into caching and cache sweepers. I put most
“slow moving” data (data that changes infrequently) into a partial that
I can cache - the left-nav menus available for a specific user, for
instance. Unless the admin changes permissions for a user, the menu that
user sees won’t change, and my app doesn’t have to regen the menus all
the time (fits my “slow moving” data rule).
If the list of models related to a given project (another of my models)
hasn’t changed, don’t re-render the partial that creates hyperlinks to
all those related models - which can be a lot of work - but show the
cached version of that partial (fits my “expensive to do” rule).