class Marten::Handlers::RecordCreate

Overview

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 class method. The schema used to perform the validation can be defined through the use of the #schema class method. 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.

Defined in:

marten/handlers/record_create.cr

Class Method Summary

Instance Method Summary

Instance methods inherited from class Marten::Handlers::Schema

context context, post post, process_invalid_schema process_invalid_schema, process_valid_schema process_valid_schema, put put, schema schema, schema_class schema_class, success_url success_url

Class methods inherited from class Marten::Handlers::Schema

schema(schema : Marten::Schema.class | Nil)
schema : Marten::Schema.class | Nil
schema
, success_route_name(success_route_name : String | Nil)
success_route_name : String | Nil
success_route_name
, success_url(success_url : String | Nil)
success_url : String | Nil
success_url

Instance methods inherited from class Marten::Handlers::Template

context context, get get

Class methods inherited from class Marten::Handlers::Template

template_name : String | Nil template_name

Instance methods inherited from module Marten::Handlers::Rendering

get_response(content) get_response, render_template(context : Hash | NamedTuple | Nil | Marten::Template::Context) render_template, render_to_response(context : Hash | NamedTuple | Nil | Marten::Template::Context) render_to_response, template_name : String template_name

Instance methods inherited from class Marten::Handlers::Base

delete delete, dispatch : Marten::HTTP::Response dispatch, get get, head(status : Int32) : HTTP::Response
head
head
, json(raw_json : String, status = 200)
json(serializable, status = 200)
json
, options options, params : Hash(String, Int16 | Int32 | Int64 | Int8 | String | UInt16 | UInt32 | UInt64 | UInt8 | UUID) params, patch patch, post post, put put, redirect(url : String, permanent = false) redirect, render(template_name : String, context : Hash | NamedTuple | Nil | Marten::Template::Context = nil, content_type = HTTP::Response::DEFAULT_CONTENT_TYPE, status = 200) render, request : Marten::HTTP::Request request, respond(content = "", content_type = HTTP::Response::DEFAULT_CONTENT_TYPE, status = 200) respond, response : Marten::HTTP::Response? response, response! response!, reverse(*args, **options)
reverse(*args, **options, &)
reverse
, trace trace

Constructor methods inherited from class Marten::Handlers::Base

new(request : HTTP::Request, params : ParamsHash)
new(request : HTTP::Request, **kwargs)
new

Class methods inherited from class Marten::Handlers::Base

http_method_names
http_method_names(*method_names : String | Symbol)
http_method_names

Instance methods inherited from module Marten::Handlers::Session

session(*args, **options)
session(*args, **options, &)
session

Instance methods inherited from module Marten::Handlers::RequestForgeryProtection

get_csrf_token get_csrf_token, referer_trusted? referer_trusted?

Instance methods inherited from module Marten::Handlers::Flash

flash(*args, **options)
flash(*args, **options, &)
flash

Instance methods inherited from module Marten::Handlers::Cookies

cookies(*args, **options)
cookies(*args, **options, &)
cookies

Macros inherited from module Marten::Handlers::Callbacks

after_dispatch(*names) after_dispatch, before_dispatch(*names) before_dispatch

Class Method Detail

def self.model(model : DB::Model.class | Nil) #

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


[View source]
def self.model : DB::Model.class | Nil #

Returns the configured model class.


[View source]

Instance Method Detail

def model : Model.class #

Returns the model used to create the new record.


[View source]
def 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.


[View source]
def record : Marten::DB::Model? #

Returns the created record upon a valid schema processing, returns nil otherwise.


[View source]
def record=(record : Marten::DB::Model | Nil) #

[View source]