Class: Superform::Rails::Form
- Includes:
- Phlex::Rails::Helpers::FormAuthenticityToken, Phlex::Rails::Helpers::URLFor
- Defined in:
- lib/superform/rails/form.rb
Overview
A Phlex::HTML view module that accepts a model and sets a Superform::Namespace
with the Object#model_name as the key and maps the object to form fields
and namespaces.
The Form::Field is a class that's meant to be extended so you can customize the Form inputs
to your applications needs. Defaults for the input, button, label, and textarea tags
are provided.
The Form component also handles Rails authenticity tokens via the authenticity_toklen_field
method and the HTTP verb via the _method_field.
Direct Known Subclasses
Constant Summary collapse
- Field =
The
Fieldclass is nested inside theFormclass so it can be easily extended to customize the form inputs for your application. For example, if you wanted to add some default classes to all your inputs and labels you could do something like:class MyForm < Superform::Rails::Form class Field < self::Field def input(**attributes) super(class: "input input-bordered", **attributes) end def label(**attributes, &block) super(class: "label", **attributes, &block) end end end Superform::Rails::Field
Instance Attribute Summary collapse
-
#model ⇒ Object
Returns the value of attribute model.
Instance Method Summary collapse
- #around_template ⇒ Object
- #build_field ⇒ Object
- #form_tag ⇒ Object
-
#initialize(model, action: nil, method: nil, **attributes) ⇒ Form
constructor
A new instance of Form.
- #key ⇒ Object
- #submit(value = submit_value, **attributes) ⇒ Object
- #view_template {|_self| ... } ⇒ Object
Constructor Details
#initialize(model, action: nil, method: nil, **attributes) ⇒ Form
Returns a new instance of Form.
51 52 53 54 55 56 57 |
# File 'lib/superform/rails/form.rb', line 51 def initialize(model, action: nil, method: nil, **attributes) @model = model @action = action @method = method @attributes = attributes @namespace = Namespace.root(key, object: model, form: self) end |
Instance Attribute Details
#model ⇒ Object
Returns the value of attribute model.
36 37 38 |
# File 'lib/superform/rails/form.rb', line 36 def model @model end |
Instance Method Details
#around_template ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/superform/rails/form.rb', line 59 def around_template(&) form_tag do authenticity_token_field _method_field super end end |
#build_field ⇒ Object
47 48 49 |
# File 'lib/superform/rails/form.rb', line 47 def build_field(...) self.class::Field.new(...) end |
#form_tag ⇒ Object
67 68 69 |
# File 'lib/superform/rails/form.rb', line 67 def form_tag(&) form action: form_action, method: form_method, **@attributes, & end |
#key ⇒ Object
83 84 85 |
# File 'lib/superform/rails/form.rb', line 83 def key @model.model_name.param_key end |
#submit(value = submit_value, **attributes) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/superform/rails/form.rb', line 75 def submit(value = submit_value, **attributes) input **attributes.merge( name: "commit", type: "submit", value: value ) end |
#view_template {|_self| ... } ⇒ Object
71 72 73 |
# File 'lib/superform/rails/form.rb', line 71 def view_template(&block) yield self if block_given? end |