Ac1c51f25718910e4a6d19ee44b5d465

Holy smokes! Maintaining these forms across my RoR app is becoming a real nightmere. I'm ready to DRY them up, but I'm struggling. I've tried looking into a custom FormBuilder, but lack of documentation and examples led me to extreme failure. I've tried putting content into partials, but this didn't seem to help much.

Ultimately, the code to display the form title and form actions are shared across all forms. Then, the code to generate field groups and labels are identical.

Sounds like an opportunity for a custom FormBuilder.

Any takers?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<%= stylesheet_link_tag 'style' %>
<%= stylesheet_link_tag site_short_name + "/stylesheet.css" %>

<div id="form_wrapper">
<% form_for(:invite, :url => { :action => 'index'} ) do |f| %>

  <div class="form_title">
	Request an Invitation
  </div>

  <div class="form_body">
	<div class="form_group_description">
		Give us your email address, and we'll send you an invitation when it's time.
  	</div>
  	<div class="form_group">
  		<div class="form_item">
  			<label for="invite_email_address"><strong>Email Address</strong></label><br/>
  			<%= f.text_field :email_address, :style => 'font-size: 16pt' %>
  		</div>
  	</div>

  	<div class="form_group_description">
		The following fields are optional, however, we recommend that you still take a moment to fill them out. This information will help us identify who you are. Enter a comment about why you are requesting an invitation, or where we met.
  	</div>
  	<div class="form_group">
  		<div class="form_item float_left">
  			<label for="invite_first_name"><strong>First Name</strong> <span class="optional">(optional)</span></label><br/>
  			<%= f.text_field :first_name, :size => '22' %>
  		</div>

  		<div class="form_item float_left">
			<label for="invite_last_name"><strong>Last Name</strong> <span class="optional">(optional)</span></label><br/>
			<%= f.text_field :last_name, :size => '22' %>
  		</div>

  		<div class="float_clear"></div>

  		<div class="form_item">
			<label for="invite_comments"><strong>Comments</strong> <span class="optional">(optional)</span></label><br/>
			<%= f.text_area :comments, :rows => 3, :cols => 49 %>
  		</div>
  	</div>
  </div>

  <div class="form_item form_actions">
	<%= submit_tag "Cancel", :onclick => 'RedBox.close(); return false;', :style => 'float:right; font-size: 12pt' %>
  	<%= submit_tag "Request Invite", :style => 'font-size: 12pt' %>
	<%= link_to_close_redbox(image_tag("close.gif", { :alt => 'Close', :border => 0, :class => 'login-box-close-icon'})) %>
  </div>

<% end %>
</div>

Refactorings

No refactoring yet !

Ac1c51f25718910e4a6d19ee44b5d465

openid.aol.com/Zanoryt

January 30, 2008, January 30, 2008 05:39, permalink

No rating. Login to rate!

85% of these forms are shared. I want to leave room for a dynamic amount of additional field groups and fields.

Something like:

<% my_form_for :invite, :url => { :action => 'index' } do |f| %>
<%= f.title "Test Form" %>
<% f.field_group do |group| %>
<%= g.text_field :name, :label => "Full Name" %>
<% end %>
<%= f.actions %>
<% end %>

would be ideal!

071bcd947c31f0e41e3c7749ae843f50

method

February 9, 2008, February 09, 2008 23:57, permalink

No rating. Login to rate!

Not sure what's wrong with partials. Say you move all that code into _form.html.erb...

1
2
3
4
5
<%= render :partial => 'form', :locals => {:f => f} %>
<%= submit "Update" %>

# from a different controller than form...
<%= render :partial => 'original/form', :locals => {:f => f} %>

Your refactoring





Format Copy from initial code

or Cancel