# Alchemy::Ajax::Form A gem for semplify form creations in alchemy. ## Installation Add this line to your application's Gemfile: ```ruby gem 'alchemy-ajax-form', github: "ArchimediaZerogroup/alchemy-ajax-form" ``` And then execute: ```bash $ bundle install ``` Or install it yourself as: ```bash $ gem install alchemy-ajax-form ``` install mjml 4.1.0 with npm globally (with sudo if necessary) ```bash $ npm install mjml@4.1.0 -g ``` ## Usage Launch the generator with this command: ```bash $ bin/rails generate custom_form name_of_form param:type ``` example: ```bash $ bin/rails generate custom_form contact_form email:string firstname:string lastname:string message:text ``` Ansawer to the questions step by step Configure element in layoutpage Restart Server ### How use without generator If you don't wont use the generator you have to follow these steps: Let's assume we want to create the form "ContactForm" #### Create Admin Controller Create Admin::ContactFormsController (/app/controllers/admin/contact_forms_controller.rb) ```ruby class Admin::ContactFormsController < Alchemy::Admin::AjaxFormsController end ``` #### Create Controller Create ContactFormsController (/app/controllers/admin/contact_forms_controller.rb) ```ruby class ContactFormsController < Alchemy::AjaxFormsController end ``` #### Create Resource Create ContactFormResource (app/lib/contact_form_resource.rb) ```ruby class ContactFormResource < Alchemy::AjaxFormResource end ``` #### Create Model Create ContactForm (app/models/contact_form.rb) ```ruby class ContactForm < Alchemy::AjaxForm validates :check_privacy, inclusion: [true, '1'] def notify_subject I18n.translate(:contact_form_notify, scope: [:ajax_form_mailer, :subjects]) end def notify_user_subject I18n.translate(:contact_form_user_notify, scope: [:ajax_form_mailer, :subjects]) end end ``` #### Create Ability Create ContactFormAbility (app/models/contact_form_ability.rb) ```ruby class ContactFormAbility include CanCan::Ability def initialize(user) if user.present? && user.is_admin? can :manage, ContactForm cannot :create, ContactForm cannot :update, ContactForm can :manage, :admin_contact_forms cannot :create, :admin_contact_forms cannot :update, :admin_contact_forms end end end ``` #### Create Migration Create CreateContactForm (db/migrate/%Y%m%d%H%M%Screate_contact_form.rb) ```ruby class CreateContactForm < ActiveRecord::Migration[5.2] def change create_table :contact_forms do |t| # insert all form fields t.boolean :check_privacy t.references :language, index: true t.timestamps end end end ``` #### Create Element Configuration Insert into file "config/alchemy/elements.yml" ```yaml - name: contact_form hint: false contents: - name: privacy_page type: EssenceText settings: linkable: true - name: recipient_notification type: EssenceText default: "example@example.it" - name: send_only type: EssenceBoolean - name: send_staff type: EssenceBoolean - name: send_user type: EssenceBoolean\n ``` #### Create Element View Create contact form view "app/views/alchemy/elements/_contact_form_view.html.erb" ```erbruby <% cache(element) do %> <%= element_view_for(element) do |el| -%> <% privacy_link = element.content_by_name(:privacy_page).essence.link privacy_title = element.content_by_name(:privacy_page).essence.body @contact_form = ContactForm.new @contact_form.alcm_element = element.id %>
<%= form_for(@contact_form, url: main_app.contact_forms_path, method: :post, remote: true, multipart: true,html:{class: "ajax_forms"}) do |f| %> <%= f.hidden_field :alcm_element %>attribute name: <%= rec.attribute_name %>