# Newbie: Corrcet use of Render :Partial for forms in CRUD?

**URL:** <https://www.ruby-forum.com/t/newbie-corrcet-use-of-render-partial-for-forms-in-crud/139386>\
**Category:** Rails\
**Created:** [June 3, 2008, 1:31pm UTC](https://www.ruby-forum.com/t/newbie-corrcet-use-of-render-partial-for-forms-in-crud/139386 "2008-06-03T13:31:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![oshuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ea5d25/32.png) [@oshuma](https://www.ruby-forum.com/u/oshuma)\
**Post date:** [June 3, 2008, 1:31pm UTC](https://www.ruby-forum.com/t/newbie-corrcet-use-of-render-partial-for-forms-in-crud/139386/1 "2008-06-03T13:31:12Z")

</div>

Cna anyone point e at any rails 2.0 tutorials on simple use of partials  
for forms in new and update CRUD operations?

Ive got the following new.html.erb and \_user.html.erb partial

First the contents of new.html.erb

# New user

\<%= error\_messages\_for :user %\>

\<% form\_for(@user) do |f| %\>

\<%= render :partial =\> @user %\>

\<%= f.submit "Create" %\>

\<% end %\>

\<%= link\_to ‘Back’, users\_path %\>

…and the form partial…

\_user.html.erb

**ImageName**  
 \<%= f.text\_field :ImageName" %\>

**Surname**  
 \<%= f.text\_field :SurName" %\>

**Forename**  
 \<%= f.text\_field :ForeName" %\>

**Title**  
 \<%= f.text\_field :Title" %\>

**Room No**  
 \<%= f.text\_field :RoomNo" %\>

**Phone No**  
 \<%= f.text\_field :PhoneNo" %\>

**Position / Group**  
 \<%= f.text\_field :Position\_Group" %\>

**Duties**  
 \<%= f.text\_field :Duties" %\>

**EMail**  
 \<%= f.text\_field :EMail" %\>

---

<div class="post-metadata">

**Author:** ![oshuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ea5d25/32.png) [@oshuma](https://www.ruby-forum.com/u/oshuma)\
**Post date:** [June 3, 2008, 6:24pm UTC](https://www.ruby-forum.com/t/newbie-corrcet-use-of-render-partial-for-forms-in-crud/139386/2 "2008-06-03T18:24:48Z")

</div>

Hi Dale, I’m new to this too… for general reference, this may be  
useful: [Flashcards, notes and learning tools online: YoYoBrain.com.](http://www.yoyobrain.com/cardboxes/preview/863) there’s info on  
rails and a tool to help you learn rails. Best, Elizabeth

On Jun 3, 6:31 am, Dale C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![oshuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ea5d25/32.png) [@oshuma](https://www.ruby-forum.com/u/oshuma)\
**Post date:** [June 3, 2008, 7:04pm UTC](https://www.ruby-forum.com/t/newbie-corrcet-use-of-render-partial-for-forms-in-crud/139386/3 "2008-06-03T19:04:08Z")

</div>

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  
application.

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  
partial.

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?  
Probably not.

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).

Divorcing that

---

<div class="post-metadata">

**Author:** ![oshuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ea5d25/32.png) [@oshuma](https://www.ruby-forum.com/u/oshuma)\
**Post date:** [June 3, 2008, 7:44pm UTC](https://www.ruby-forum.com/t/newbie-corrcet-use-of-render-partial-for-forms-in-crud/139386/4 "2008-06-03T19:44:08Z")

</div>

So is this not working for you? The only thing I might want to add is  
to pass the form helper object ‘f’ to the partial in the :locals hash.  
Not sure if that’s necessary…

---

<div class="post-metadata">

**Author:** ![oshuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/ea5d25/32.png) [@oshuma](https://www.ruby-forum.com/u/oshuma)\
**Post date:** [June 3, 2008, 7:06pm UTC](https://www.ruby-forum.com/t/newbie-corrcet-use-of-render-partial-for-forms-in-crud/139386/5 "2008-06-03T19:06:55Z")

</div>

Ar Chron wrote:

> Divorcing that

just ignore that incomplete thought… ☹
