class

Marten::Handlers::RecordCreate

Inherits Marten::Handlers::Schema / Marten::Handlers::Schema::Callbacks / Marten::Handlers::Template / Marten::Handlers::Rendering / Marten::Handlers::Base / Marten::Handlers::XFrameOptions / Marten::Handlers::ContentSecurityPolicy / Marten::Handlers::Session / Marten::Handlers::RequestForgeryProtection / Marten::Handlers::Flash / Marten::Handlers::Cookies / Marten::Handlers::ExceptionHandling / Marten::Handlers::Callbacks / Marten::Core::DebugModeLoggable / Reference / Object

Handler allowing to create a new model record by processing a schema.

This handler can be used to process a form, validate its data through the use of a schema, and create a record by using the validated data. It is expected that the handler will be accessed through a GET request first: when this happens the configured template is rendered and displayed, and the configured schema which is initialized can be accessed from the template context in order to render a form for example. When the form is submitted via a POST request, the configured schema is validated using the form data. If the data is valid, the corresponding model record is created and the handler returns an HTTP redirect to a configured success URL.

class MyFormHandler < Marten::Handlers::RecordCreate
  model MyModel
  schema MyFormSchema
  template_name "my_form.html"
  success_route_name "my_form_success"
end

It should be noted that the redirect response issued will be a 302 (found).

The model class used to create the new record can be configured through the use of the #model macro. The schema used to perform the validation can be defined through the use of the #schema macro. Alternatively, the #schema_class method can also be overridden to dynamically define the schema class as part of the request handling.

The #template_name class method allows to define the name of the template to use to render the schema while the #success_route_name method can be used to specify the name of a route to redirect to once the schema has been validated. Alternatively, the #sucess_url class method can be used to provide a raw URL to redirect to. The same method can also be overridden at the instance level in order to rely on a custom logic to generate the sucess URL to redirect to.

Instance methods

model

Returns the model used to create the new record.

Source
prepare_record_attributes

Prepares the model attributes from the validated schema data.

This method can be overridden to customize how schema data is transformed into model attributes. By default, the method returns the data that was validated by the schema as is.

Source
process_valid_schema

Produces the response when the processed schema is valid.

By default, this will create the new record and return a 302 redirect targetting the configured success URL.

Source
record
Source
record=(r)
Source
save_record

Saves the record after the schema has been validated.

This method can be overridden in order to change the way the record is saved from the validated data.

Source

Macros

model(model_klass)

Allows to configure the model class that should be used to create the new record.

Source