AndyV said the following on 01/02/08 04:24 PM:
@Anton
Andrew has the right idea. new/edit can be combined into a common
data partial that can be rendered by both (and if you don’t mind input
elements in your show you can combine that, too). The extra use of
helpers doesn’t make sense to me, though.
Agreed. And they aren’t DRY.
The main place I’d differ is in what’s passed to the partial.
Agreed. See below.
As
constructed it is passing the result of a fields_for call (there’s a
‘hidden’ one in the form_for call). I’d recommend just passing the
object in question instead.
Agreed.
You’ve just described what I had a result of running the ‘scaffold_form’
generator.
I had the edit/new in one partial
_form.html.erb:
Name
<%= f.text_field ‘name’ %>
…
and
edit.html.erb
<% form_for(@person) do |f| %>
<%= render :partial => 'form' , :locals=>{:f => f}%>
<%= submit_tag 'Save' %>
<% end %>
new.html.erb
<% form_for(@person) do |f| %>
<%= render :partial => 'form' , :locals=>{:f => f}%>
<%= submit_tag "Add" %>
<% end %>
But ‘scaffold_form’ didn’t generate the details for the view.html.erb
I’ve tried ‘fields_for’:
<% fields_for(@web) do |f| %>
<% render :partial => 'form' , :locals=>{:f => f}%>
<% end %>
Please review the source for ‘field_for’.
It produces a FORM. That is the fields are editable, there just isn’t a
“submit” button.
The following assume that you’re setting @person in the controller…
new.html.erb
<% form_for :person, :url=>people_path do |f| %>
<%= render :partial => ‘data’, :locals=>{:person=>@person} %>
<% end %>
See above.
edit.html.erb
[…]
[…] The broader point is
that you’ve written one file that captures all the common input
elements and no longer have to worry about keeping the common set in
sync among the templates that use them.
Yes, I’ve understood and sought that from the beginning.
My point is trying to make that “one file that captures” all the fields,
regardless of whether they are being used in and input form or as a
display, in one file.
One file for layout for ALL of [‘new’, ‘edit’, ‘show’ ]
Please Andy, we’re just going over and over the same stuff. I know that
all this works for [‘edit’, ‘new’]; I’m trying to find out how to make
it work for ‘show’ as well.
Andrew S. had a way, but I didn’t like it
–
Put your hand on a hot stove for a minute, and it seems like an hour.
Sit with a pretty girl for an hour, and it seems like a minute. THAT’S
relativity.
–Albert Einstein