# Extend form\_for

**URL:** <https://www.ruby-forum.com/t/extend-form-for/158097>\
**Category:** Rails\
**Created:** [January 19, 2009, 11:58am UTC](https://www.ruby-forum.com/t/extend-form-for/158097 "2009-01-19T11:58:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tatyree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f14d63/32.png) [@tatyree](https://www.ruby-forum.com/u/tatyree)\
**Post date:** [January 19, 2009, 11:58am UTC](https://www.ruby-forum.com/t/extend-form-for/158097/1 "2009-01-19T11:58:00Z")

</div>

I’m trying to extend form\_for to add a hidden field to certain forms  
but not others. I realize that I need to push it on to the end of the  
&proc, but have yet to find an elegant way of doing it and was  
wondering if anyone had any insights?

This is as close as I’ve come to doing it without having to copy and  
paste the whole form\_for method in to the else:

module ActionView  
module Helpers  
module FormHelper  
def form\_for\_with\_session\_included(record\_or\_name\_or\_array,  
\*args, &proc)  
if !bad\_request?  
form\_for\_without\_session\_included(record\_or\_name\_or\_array,  
\*args, &proc)  
else  
proc\_with\_session = Proc.new{ proc.call.concat  
(hidden\_field\_tag(:\_session\_id, session.session\_id, :id =\> nil)) }  
form\_for\_without\_session\_included(record\_or\_name\_or\_array,  
\*args, &proc\_with\_session)  
end  
end  
alias\_method\_chain :form\_for, :session\_included  
end  
end  
end

The problem seems to comes in in that the proc holds all the  
FormHelper fields. These expect to wake up inside the form\_for object,  
so they do a lot of NoMethodError complaining when they get woken up  
like this.

The only option that I’ve found that works is the aforementioned  
simian solution of copying the whole form\_for method in to the else  
and adding an additional concat with my hidden\_field. This just  
smells too strongly of future maintenance headache, and so I’d like a  
much neater solution.

I’m pretty new to procs beyond very simple usage, so any help or  
suggestions would be greatly appreciated.

Thanks,  
Todd

(Yes, that’s cookie-based sessions being overridden. Mobile phone  
browsers are the spawn of satan).

---

<div class="post-metadata">

**Author:** ![tatyree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f14d63/32.png) [@tatyree](https://www.ruby-forum.com/u/tatyree)\
**Post date:** [January 19, 2009, 12:16pm UTC](https://www.ruby-forum.com/t/extend-form-for/158097/2 "2009-01-19T12:16:13Z")

</div>

On 19 Jan 2009, at 00:43, tatyree wrote:

> module Helpers  
> \*args, &proc\_with\_session)  
> end  
> end  
> alias\_method\_chain :form\_for, :session\_included  
> end  
> end  
> end

I would have thought something likw  
form\_for\_without\_session\_included(record\_or\_name\_or\_array, \*args) do |f|  
hidden\_field\_tag …  
proc.call(f)  
end

would have done the trick. Monkey patching this in seems a little  
unnecessary. You could just as easily write a helper\_called  
my\_form\_for and call that when you want this extra behaviour

Fred
