I have set up a tableless model as described in the Rails 7 guides:
class ContactForm
include ActiveModel::Model
attr_accessor :name, :email, :message
validates :name, :email, :message, presence: true
end
I have set up an action to check if the submitted content is valid and send the email if it is:
def contact_process
@contact_form = ContactForm.new(contact_form_params)
if @contact_form.valid?
UserMailer.with(@contact_form).contact_form.deliver_later
redirect_to contact_path
else
render :contact
end
end
When there are errors and the contact template is rendered again @contact_form seems to be a blank ContactForm instance and for example @contact_form.errors.count returns 0 even though it was printing the correct number in the console just before the render command.