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

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

<h1>New user</h1>

<%= error_messages_for :user %>

<% form_for(@user) do |f| %>

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

  <p>
    <%= f.submit "Create" %>
  </p>
<% end %>

<%= link_to 'Back', users_path %>

....and the form partial....

_user.html.erb

  <p>
    <b>ImageName</b><br />
      <%= f.text_field :ImageName" %>
  </p>

  <p>
    <b>Surname</b><br />
      <%= f.text_field :SurName" %>
  </p>

  <p>
    <b>Forename</b><br />
      <%= f.text_field :ForeName" %>
  </p>

  <p>
    <b>Title</b><br />
      <%= f.text_field :Title" %>
  </p>

  <p>
    <b>Room No</b><br />
      <%= f.text_field :RoomNo" %>
  </p>

  <p>
    <b>Phone No</b><br />
      <%= f.text_field :PhoneNo" %>
  </p>

  <p>
    <b>Position / Group</b><br />
      <%= f.text_field :Position_Group" %>
  </p>

  <p>
    <b>Duties</b><br />
      <%= f.text_field :Duties" %>
  </p>

  <p>
    <b>EMail</b><br />
      <%= f.text_field :EMail" %>
  </p>

Hi Dale, I'm new to this too... for general reference, this may be
useful: http://www.yoyobrain.com/cardboxes/preview/863 there's info on
rails and a tool to help you learn rails. Best, Elizabeth

Dale Cunnigham 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

Ar Chron wrote:

Divorcing that

just ignore that incomplete thought... :frowning:

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