Insert a non-input row into a Formtasic form

前端 未结 4 756
谎友^
谎友^ 2020-12-13 21:29

I am using Formtastic 2.1.1 in Rails 3.2 (with Active Admin) and I want to insert a row into my form that does not have an input field present. Is this poss

相关标签:
4条回答
  • 2020-12-13 21:46

    Here's a slightly simplified form of @arsen7's answer:

    f.form_buffers.last << 
      "<p>Activate interlock, dynatherms connected</p>".html_safe
    

    which in my form looks like this:

    Simple HTML insert

    And here's one that mimics ActiveAdmin's default style:

    f.form_buffers.last << (<<END
    <li class="string input optional stringish">
      <label class="label">Activate interlock</label>
      <div style="display: inline-block;">Dynatherms connected</div>
    </li>
    END
    ).html_safe
    

    which looks like this:

    ActiveAdmin-style HTML insert

    0 讨论(0)
  • 2020-12-13 21:56

    To insert any custom code in any place, you may use f.form_buffers.last:

    form do |f|
      f.form_buffers.last << "<p>Hello world!</p>".html_safe # The simple way
    
      ft = f.template # just a helper variable
    
      f.inputs "Foo" do
        f.input :title
    
        f.form_buffers.last << ft.content_tag(:li) do
          ft.content_tag(:p, "Hello again!") +
              ft.tag(:input, type: :hidden, name: "bar[]", value: "baz")
        end
        f.input :path
      end
    end
    

    Just be careful about the HTML structure. If you call this from f.inputs block, your code will be placed inside an <ol> element. On the "form" level, you are inside a <form> element.

    A little warning: As with any "undocumented feature" this method may change without warning in any new release.

    0 讨论(0)
  • 2020-12-13 21:58

    IDK if this is still something people search for but it was for me. Since form_buffers has been deprecated, I did the following and it worked out beautifully (with some CSS help).

    form do |f|
    
      f.inputs "Model Info" do
        f.input :title
        f.input :published
        f.input :path
        li "Your important text here", class: "some_class"
      end
    end
    

    this outputs:

    <li class="some_class">
      Your important text here
    </li>
    

    Got this idea from the docs after a too long Google search here FORMS and here ARBRE.

    0 讨论(0)
  • 2020-12-13 22:11

    Figured this out myself. I just needed to insert the html without any method calls, like so:

    form do |f|
    
      f.inputs "Model Info" do
        f.input :title
        f.input :published
        f.input :path
      end
    
      f.inputs "Custom HTML Stuff" do
        "<div id=\"element-name\">
          Some kind of content
        </div>".html_safe
      end
    
    end
    
    0 讨论(0)
提交回复
热议问题