# How can I "DRYize" this method?

**URL:** <https://www.ruby-forum.com/t/how-can-i-dryize-this-method/158276>\
**Category:** Rails\
**Created:** [January 21, 2009, 3:40pm UTC](https://www.ruby-forum.com/t/how-can-i-dryize-this-method/158276 "2009-01-21T15:40:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![flaubert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/858c86/32.png) [@flaubert](https://www.ruby-forum.com/u/flaubert)\
**Post date:** [January 21, 2009, 3:40pm UTC](https://www.ruby-forum.com/t/how-can-i-dryize-this-method/158276/1 "2009-01-21T15:40:43Z")

</div>

Hi all,

well I have this helper in my form, but it isn’t DRY, as you see:

\<%= f.text\_field(:birth\_date, :size =\> ‘10’, :maxlength =\>  
‘10’, :class =\> “format-d-m-y divider-slash split-date”, :value =\>  
(@person.birth\_date.nil? ? ‘’: @person.birth\_date)) %\>

so, @person and :birth\_date must be dymamic in a helper.

How can I write those on a helper?

---

<div class="post-metadata">

**Author:** ![flaubert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/858c86/32.png) [@flaubert](https://www.ruby-forum.com/u/flaubert)\
**Post date:** [January 21, 2009, 3:49pm UTC](https://www.ruby-forum.com/t/how-can-i-dryize-this-method/158276/2 "2009-01-21T15:49:33Z")

</div>

On Wed, Jan 21, 2009 at 4:39 PM, flaubert [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> so, @person and :birth\_date must be dymamic in a helper.
> 
> How can I write those on a helper?
> 
> >

helper:  
def date\_field(form, method, model)  
form.text\_field(method,  
:size =\> 10,  
:maxlength =\> 10,  
:class =\> ‘format-d-m-y divider-slash  
split-date’,  
:value =\> (model.send(method).nil? ? ‘’ :  
model.send(method))  
)  
end

view:  
\<%= date\_field(f, :birth\_date, @person) %\>

You could also create a custom form builder and add a date\_field method  
there to get the following in your view:  
\<%= f.date\_field :birth\_date %\>

–  
Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain

---

<div class="post-metadata">

**Author:** ![flaubert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/858c86/32.png) [@flaubert](https://www.ruby-forum.com/u/flaubert)\
**Post date:** [January 21, 2009, 3:56pm UTC](https://www.ruby-forum.com/t/how-can-i-dryize-this-method/158276/3 "2009-01-21T15:56:37Z")

</div>

Hi –

On Wed, 21 Jan 2009, flaubert wrote:

> so, @person and :birth\_date must be dymamic in a helper.  
> What does your form\_for line look like? If you’ve got:

form\_for :person … do |f|  
f.text\_field(:birth\_date)

(or some small variations on that) you’ll get @person.birth\_date in  
the field automatically.

David

–  
David A. Black / Ruby Power and Light, LLC  
Ruby/Rails consulting & training: [http://www.rubypal.com](http://www.rubypal.com)  
Coming in 2009: The Well-Grounded Rubyist ([The Well-Grounded Rubyist](http://manning.com/black2))

[http://www.wishsight.com](http://www.wishsight.com) =\> Independent, social wishlist management!

---

<div class="post-metadata">

**Author:** ![flaubert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/858c86/32.png) [@flaubert](https://www.ruby-forum.com/u/flaubert)\
**Post date:** [January 21, 2009, 4:06pm UTC](https://www.ruby-forum.com/t/how-can-i-dryize-this-method/158276/4 "2009-01-21T16:06:04Z")

</div>

tnks guy! it works, I tried exactly this, but except the “form” var.

very tnks!

On 21 jan, 11:49, Andrew T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
