Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>EDIT: OK, my first pass on this didn't work because you can't define a value in an initializer that you're later going to pass in from a controller. So you can go about this one of two ways. You can define a <code>CustomFormBuilder</code> class - put it in an initializer - </p> <pre><code>class CustomFormBuilder &lt; ActionView::Helpers::FormBuilder def submit(value, options = {}) @template.hidden_field_tag(options.delete(:custom_param)) + super end end </code></pre> <p>Then pass the <code>:builder</code> option to <code>form_for</code></p> <pre><code>form_for @whatever, builder: CustomFormBuilder </code></pre> <p>and assuming you call <code>submit(value, options)</code> in the form, where options includes your custom_param, it will be overwritten by the custom method that inserts your hidden field with the value you want.</p> <p>The alternative is to monkey patch monkey patch FormTagHelper instead:</p> <pre><code>module ActionView::Helpers::FormTagHelper def extra_tags_for_form_with_custom_param(html_options) hidden_field_tag(html_options.delete('custom_param') + extra_tags_for_form_without_custom_param(html_options) end alias_method_chain :extra_tags_for_form, :custom_param end </code></pre> <p>That's tweaking the code <code>form_for</code> uses to insert the authenticity token at the top of every form. Now you can pass that custom param to <code>form_for</code> after setting it as an instance variable in the controller:</p> <pre><code>form_for @object, custom_param: @custom_param do |f| </code></pre> <hr> <p>If you are using Ruby 2.0+ then you can use module prepend instead of the deprecated alias_method_chain e.g.</p> <pre><code>module CustomParamFormPatch private def extra_tags_for_form(html_options) hidden_field_tag(html_options.delete('custom_param')) + super end end ActionView::Base.prepend(CustomParamFormPatch) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload