DRYing up form tags


#1

Hi,

I find that I have the following appear a lot inside forms in my app:

<% names = SomeAssociatedModel.find(:all, :order => ‘name’)
<%= f.collection_select :some_id, names, :id, :name %>

Which is basically a select list of names vs. id. I use it when I relate
between associated models (with has_many and belongs_to). I would like
to write a custom tag for it, something like:

<%= f.names_select :some_id, object_to_select_from %> or similar. This
way, I could use it when, say, editing cities that are associated with
states:

<%= f.names_select :state_id, State %>

I also have a select that’s yes/no:

<%= f.select :is_active, [[‘yes’,true],[‘no’,false]] %>

that I’d like to turn to:

<%= f.yes_no_select :is_active %>

Any pointers will surely help - thanks.


#2

Instead of writting custom tags, you could write a custom form builder
with methods for those fields ->
http://onrails.org/articles/2008/06/13/advanced-rails-studio-custom-form-builder

Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/
(en)

On Mon, Jan 26, 2009 at 7:25 AM, Shilo A.