Marten::Handlers::RecordUpdate
Inherits Marten::Handlers::RecordRetrieving / 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 update a 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 update an existing 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 model record that was retrieved is updated and the handler returns an HTTP redirect to a configured success URL.
class MyFormHandlers < Marten::Handlers::RecordUpdate
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 update the 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.
Class methods
Returns the name of the model field that will be used to retrieve the record (defaults to pk).
Allows to configure the name to use to include the model record into the template context.
Returns the name to use to include the model record into the template context (defaults to record).
Instance methods
Returns a hash of initial data, computed from the considered record, to prepare the schema.
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 validated data as is, but only the fields that are part of the model are included.
Produces the response when the processed schema is valid.
By default, this will return a 302 redirect targetting the configured success URL.
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.