Forum: Ruby on Rails Extend form_for

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
tatyree (Guest)
on 2009-01-19 12:58
(Received via mailing list)
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?
*args, &proc)
          proc_with_session ={
(hidden_field_tag(:_session_id, session.session_id, :id => nil)) }
*args, &proc_with_session)
      alias_method_chain :form_for, :session_included

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.


(Yes, that's cookie-based sessions being overridden.  Mobile phone
browsers are the spawn of satan).
Frederick C. (Guest)
on 2009-01-19 13:16
(Received via mailing list)
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 ....

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

This topic is locked and can not be replied to.