Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, your idea to isolate this code in a gem/railtie/engine is excellent. I think your best bet may be to monkey patch the form_for method and stick in the extra field. Regarding the on-submit trigger, if you're on Rails 3.1 and are using the asset pipeline, you can have the gem serve javascript as well, although that would require a small change in your <code>application.js</code> to require the gem's js file, e.g. <code>require 'promo/application.js</code>, if the gem were called "promo".</p> <p>Take a look at the docs for <a href="http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for-label-Customized+form+builders" rel="nofollow">Customized Form Builder</a></p> <p>Here's some rough idea how this could work, although I haven't tried this code. I'd put this in the promo.rb file that sub-classes <code>Railtie</code> or <code>Engine</code>.</p> <pre><code>ActiveSupport.on_load(:action_view) do module ActionView module Helpers module FormHelper extend ActiveSupport::Concern included do alias_method_chain :form_for, :promo_code end module InstanceMethods def form_for_with_promo_code(record, options = {}, &amp;proc) output = form_for_without_promo_code(record, options.merge(builder: FormBuilderWithPromoCode), proc) # See file: actionpack-3.1.3/lib/action_view/helpers/form_helper.rb for details # the output will have "&lt;/form&gt;" as the last thing, strip that off here and inject your input field promo_field = content_tag :input, name: 'promo_code' # you can also run this through the proc if you want access to the object output.sub(%r{&lt;/form&gt;$},promo_field+'&lt;/form&gt;') end end end end end end </code></pre> <p>Down the road, esp. if your marketing department may run more campaigns, you may even want to change the app's forms, to point to a specific builder that you can override from a gem without the monkey patching here.</p>
    singulars
    1. This table or related slice is empty.
    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