Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Append markup after form #856

Closed
mcasimir opened this Issue · 1 comment

2 participants

@mcasimir

Sometimes is useful to append markup after the form end tag, example:

# form.html.erb
<%= f.input :photo, :as => :modal %>
# modal_input.rb
class ModalInput < Formtastic...

  def to_html
       #  create an hidden_input and a button to fire the dialog
  end

  def append_after_form
   <<-eos        
    <div class="modal" id="pick_a_photo_modal">
         <form data-remote="true" action="some other action than the current formtastic form">
              <input name="q" type="text"/>
              <input type="submit" value="Search"/>
        </form>
         <ul id="ajax_populated_list"><li>Search a tag to choose photos</li></ul>
    </div>
   eos
  end
end

Is there a simple way to do this in Formtastic? Other options are:

  • rely on 'user' code issuing something like content_for(:user_defined_hook)
  • rely on javascript to append something to body: $(document).ready... $("body").append(<%=j render ...%>)

Update: a very very simple workaround

<%= f.input :photo, :as => :photo_chooser %>
class PhotoChooserInput < FormtasticBootstrap::Inputs::HiddenInput

  def to_html
    template.after_current_form(:photo_chooser, :replace => true) << template.render(:partial => "support/photos/chooser")
    super << template.render(:partial => "support/photos/input")
  end

end 
module FormDecoratorHelper

  def after_current_form(key = :default, options = {:replace => false})
    reset_after_current_form_hash! if @after_current_form.nil?
    if options[:replace]
      @after_current_form[key] = ""
    end

    @after_current_form[key]
  end

  def reset_after_current_form_hash!
    @after_current_form = ActiveSupport::OrderedHash.new
    @after_current_form[:default] = ""
  end

  def semantic_form_for(record_or_name_or_array, *args, &proc)
    reset_after_current_form_hash!
    super(record_or_name_or_array, *args, &proc) << @after_current_form.map { |k, v| v }.join().html_safe
  end

end
@justinfrench

+1 for the work-around rather than adding more features to Formtastic :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.