Class: Locomotive::Liquid::Tags::ModelForm

Inherits:
Solid::Block
  • Object
show all
Defined in:
lib/locomotive/liquid/tags/model_form.rb

Overview

Display the form html tag with the appropriate hidden fields in order to create a content entry from a public site. It handles callbacks, csrf and target url out of the box.

Usage:

model_form ‘newsletter_addresses’ %

<input type='text' name='content[email]' />
 <input type='submit' value='Add' />

endform_form %

model_form ‘newsletter_addresses’, class: ‘a-css-class’, success: ‘www.google.fr’, error: ‘/error’ %…endform_form %

Instance Method Summary collapse

Instance Method Details

#callbacks_html(options) ⇒ Object



44
45
46
47
48
# File 'lib/locomotive/liquid/tags/model_form.rb', line 44

def callbacks_html(options)
  options.slice(:success, :error).map do |(name, value)|
    html_tag :input, type: 'hidden', name: "#{name}_callback", value: value
  end.join('')
end

#content_type_html(name) ⇒ Object



33
34
35
# File 'lib/locomotive/liquid/tags/model_form.rb', line 33

def content_type_html(name)
  html_tag :input, type: 'hidden', name: 'content_type_slug', value: name
end

#csrf_htmlObject



37
38
39
40
41
42
# File 'lib/locomotive/liquid/tags/model_form.rb', line 37

def csrf_html
  name  = controller.send(:request_forgery_protection_token).to_s
  value = controller.send(:form_authenticity_token)

  html_tag :input, type: 'hidden', name: name, value: value
end

#display(*options, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/locomotive/liquid/tags/model_form.rb', line 22

def display(*options, &block)
  name    = options.shift
  options = options.shift || {}

  form_attributes = { method: 'POST', enctype: 'multipart/form-data' }.merge(options.slice(:id, :class))

   :form,
    content_type_html(name) + csrf_html + callbacks_html(options) + yield,
    form_attributes
end