# Is it possible to have forms that are too DRY?

**URL:** <https://www.ruby-forum.com/t/is-it-possible-to-have-forms-that-are-too-dry/149260>\
**Category:** Rails\
**Created:** [September 23, 2008, 12:52am UTC](https://www.ruby-forum.com/t/is-it-possible-to-have-forms-that-are-too-dry/149260 "2008-09-23T00:52:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [September 23, 2008, 12:52am UTC](https://www.ruby-forum.com/t/is-it-possible-to-have-forms-that-are-too-dry/149260/1 "2008-09-23T00:52:03Z")

</div>

Hi.

I’ve got a form that I’m using with polymorphic associations. It’s  
super basic. The form fields will be used in multiple places, but the  
hidden polymorphic fields will change with each model that needs them.

Currently in views/books/new.html.erb  
\<% form\_for(@review) do |f| %\>  
**Title** \<%= f.text\_field :title %\>  
**Body** \<%= f.text\_area :body %\>  
\<%= f.hidden\_field :reviewable\_type, :value =\> ‘Book’ %\>  
\<%= f.hidden\_field :reviewable\_id, :value =\> @book.id %\>  
\<%= f.submit “Submit” %\>  
\<% end %\>

Currently in views/reviews/\_form.rhtml  
\<% form\_for(@review) do |f| %\>  
**Title** \<%= f.text\_field :title %\>  
**Body** \<%= f.text\_area :body %\>  
\<%= f.submit “Update” %\>  
\<% end %\>

Is it better to just have 2 fields in a form partial in views/reviews  
and then have the other required fields in the appropriate views?

If I DRY them up, they would look like this.

In views/books/new.html.erb  
\<% form\_for(@review) do |f| %\>

\<%= render :partial =\> ‘reviews/form’, :locals=\>{:f =\> f} %\>

\<%= f.hidden\_field :reviewable\_type, :value =\> ‘Book’ %\>  
\<%= f.hidden\_field :reviewable\_id, :value =\> @book.id %\>  
\<%= f.submit “Submit” %\>  
\<% end %\>

In views/reviews/\_form.rhtml  
**Title** \<%= f.text\_field :title %\>  
**Body** \<%= f.text\_area :body %\>

Thanks for the great guidance!

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [September 23, 2008, 5:09am UTC](https://www.ruby-forum.com/t/is-it-possible-to-have-forms-that-are-too-dry/149260/2 "2008-09-23T05:09:33Z")

</div>

On Mon, Sep 22, 2008 at 3:52 PM, Becca G.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> **Body** \<%= f.text\_area :body %\>  
> \<%= f.submit “Update” %\>  
> \<% form\_for(@review) do |f| %\>  
> **Title** \<%= f.text\_field :title %\>  
> **Body** \<%= f.text\_area :body %\>
> 
> Thanks for the great guidance!

Becca,

It looks like you’re on the right track, but you might want to  
familiarize yourself with nested resources/routes in Ruby on Rails.  
You shouldn’t need to specify the type/id as hidden fields like this.

In your routes, you can do something like:

map.resources :books, :has\_many =\> [::reviews]

Then where you want to show the review form, you could do something  
like…

\<% form\_for :book, :url =\> book\_reviews\_path( @book ) do |f| %\>

When the form submits, you should see the book\_id in the params hash,  
which you can use to connect the dots.

Good luck!

Cheers,  
Robby

–  
Robby R.  
Chief Evangelist, Partner

PLANET ARGON, LLC  
design // development // hosting

> **[Planet Argon](https://www.planetargon.com/)**
>
> Planet Argon is a Ruby on Rails development agency specializing in maintenance and support for existing Rails apps. Based in Portland, OR, Planet Argon has been a member of the Rails community since 2004.

[http://www.robbyonrails.com/](http://www.robbyonrails.com/)  
aim: planetargon

+1 503 445 2457  
+1 877 55 ARGON [toll free]  
+1 815 642 4068 [fax]

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [September 23, 2008, 3:47pm UTC](https://www.ruby-forum.com/t/is-it-possible-to-have-forms-that-are-too-dry/149260/3 "2008-09-23T15:47:51Z")

</div>

What I’ve found is that DRY is good until it leads to the point of  
obscurity…

I use a standard partial to handle the hidden fields for routing users  
to the appropriate place after an add or update (if you add a B from A,  
you should go back to A. if you added the B from C, you go back to C,  
etc).

As long as the partials are named descriptively and can lead another  
developer easily to the correct source, "DRY"ness is in the eye of the  
beholder…

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [September 23, 2008, 11:07pm UTC](https://www.ruby-forum.com/t/is-it-possible-to-have-forms-that-are-too-dry/149260/4 "2008-09-23T23:07:17Z")

</div>

Robby R. wrote:

> In your routes, you can do something like:
> 
> map.resources :books, :has\_many =\> [::reviews]
> 
> Then where you want to show the review form, you could do something  
> like…
> 
> \<% form\_for :book, :url =\> book\_reviews\_path( @book ) do |f| %\>
> 
> When the form submits, you should see the book\_id in the params hash,  
> which you can use to connect the dots.

I have this in my routes file  
map.resources :books, :has\_many =\> [:reviews]

But I wasn’t able to get the form\_for to work for me. Do I have to  
include an action to get this to work properly or do I need to revise  
the above syntax to get that to work properly.

I currently have an error on this line \<%= f.text\_field :title %\> with  
the error message of" undefined method `title’".

Thanks.
