Custom form builders and form_tag?

Is it possible to use a custom form builder with form_tag? It would
be nice to impose the same look and feel for all my forms, whether or
not they are model forms.

On Jan 15, 4:30 pm, bigbanger [email protected] wrote:

Is it possible to use a custom form builder with form_tag? It would
be nice to impose the same look and feel for all my forms, whether or
not they are model forms.

OK, so it appears that form_tag doesn’t support form builders. So how
do most Rails applications keep their non-model forms DRY?

On Jan 16, 2008, at 12:02 PM, bigbanger wrote:

On Jan 15, 4:30 pm, bigbanger [email protected] wrote:

Is it possible to use a custom form builder with form_tag? It would
be nice to impose the same look and feel for all my forms, whether or
not they are model forms.

OK, so it appears that form_tag doesn’t support form builders. So how
do most Rails applications keep their non-model forms DRY?

Why would you have a form and not a model? You don’t have to make
that model class inherit from ActiveRecord::Base and be associated
with the database. Create a class that has the fields (attr_accessor)
that you need and use form_for.

(Full disclosure: I haven’t done this, but I’d be surprised if it
didn’t work.)

From

at the bottom of the entry.

Customized form builders
You can also build forms using a customized FormBuilder class.
Subclass FormBuilder and override or define some more helpers, then
use your custom builder. For example, let‘s say you made a helper to
automatically add labels to form inputs.
<% form_for :person, @person, :url => { :action =>
“update” }, :builder => LabellingFormBuilder do |f| %> <%=
f.text_field :first_name %> <%= f.text_field :last_name %> <%=
text_area :person, :biography %> <%= check_box_tag “person[admin]”,
@person.company.admin? %> <% end %>
In many cases you will want to wrap the above in another helper, so
you could do something like the following:
def labelled_form_for(name, object, options, &proc) form_for(name,
object, options.merge(:builder => LabellingFormBuiler), &proc) end
If you don‘t need to attach a form to a model instance, then check out
FormTagHelper#form_tag.

Does that help you out?

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

On Jan 16, 10:05 am, Rob B. [email protected]
wrote:

Why would you have a form and not a model? You don’t have to make
that model class inherit from ActiveRecord::Base and be associated
with the database. Create a class that has the fields (attr_accessor)
that you need and use form_for.

(Full disclosure: I haven’t done this, but I’d be surprised if it
didn’t work.)

Rob,

I thought about doing something like that, but wondered if there was a
better way. But I think it should work and I’ll give it a try and
post back.

Thanks for the suggestion.

I get the ‘form_for’
I get ‘partials’

But if I have a partial like

<%= f.text_field :first_name %>
<%= f.text_field :last_name %>
<%= text_area :person, :biography %>
<%= check_box_tag "person[admin]", @person.company.admin? %>

which I have been wrapping for my ‘edit.rhtml’ with a

<% form_for(@person) do |f| %>
<%= render :partial => ‘form’ , :locals=>{:f => f}%>
<%= submit_tag ‘Save’ %>
<% end %>

Can I use that same partial/layout for a view?


There are two ways to slide easily through life: to believe
everything or to doubt everything; both ways save us from
thinking. – Alfred Korzybski