Models normally represents tables in database. Each model object is associated with one row in the table.

But sometimes we want to create models that are not associated with any table. For example, we want a user to fill a form and perform some task instead of saving the data to the database.

Here is an example.

The user will fill up a form providing his name, email etc. and click submit. The data is captured by controller and an email will be sent to the address filled by user in the form.

  1. Let us create a new project named tableless with the command:
    rails new tableless
  2. Next, we will generate a controller with two methods: capture and send.
     rails generate controller email capture sendemail
  3. Next we will create a mailer that will use Mandrill to send emails. Click here for a detailed post on Mandrill.
     rails generate mailer mandrill_mailer
  4. We will then put our Mandrill data in config/environment/development.rb file:
    config.action_mailer.delivery_method = :smtp
    config.action_mailer.smtp_settings = {
        :address => 'smtp.mandrillapp.com',
        :port => '587',
        :authentication => :plain,
        :user_name => 'your_email_used_for_registration_in_mandrill',
        :password => 'your_mandrill_key',
        :domain => 'localhost',
        :enable_starttls_auto => true
    }
  5. Now add following code to the app/mailers/mandrill_mailer.rb file:
    class MandrillMailer < ActionMailer::Base
      default(
          :from => 'your_email',
          :reply_to => 'your_email'
      )
      def send_email(email, name, subject, message) 
         @name = name # this will be passed to the view file 
         @message = message
         mail(to: email, subject: subject) 
      end 
    end
  6. Next, we will create a model for our email. Execute the following line on command prompt:
    rails generate model email
  7. Now add following code to the file app/models/email.rb:
  8. class Email
      include ActiveModel::Model
    
      attr_accessor :name
      attr_accessor :to_email
      attr_accessor :subject
      attr_accessor :message
    
    end
  9. Since we don’t want to associate our model with database, we can remove the file: db/migrate/xxxxx_create_emails.rb
  10. Now, we will create form to capture user data. Edit the file app/views/email/capture.html.erb with following code:
    <h1>Please fill up the form</h1>
    <%= form_for @email, :url => {:action => 'sendemail'} do |f| %>
      <p>
         Name<br/><%= f.text_field 'name' %>
      </p>
      <p>
        Email<br/><%= f.text_field 'to_email' %>
      </p>
      <p>
        Subject<br/><%= f.text_field 'subject' %>
      </p>
      <p>
        Message<br/><%= f.text_area 'message' %>
      </p>
      <p>
        <%= f.submit "Submit Data" %>
      </p>
    <% end %>
  11. Next, we will edit our view file app/views/email/sendemail.html.erb to:
    <h1>Your data has been submitted to your email</h1>
  12. Now, we will create a template for our email. Create the file app/views/mandril_mailer/send_email.html.rb with following code:
    <html>
    <body>
        Hello <%=@name%>
        <hr/>
        <%=@message%>
    </body>
    </html>
  13. Lastly we will update our app/controllers/email_controller.rb file with following code:
    class EmailController < ApplicationController
       def capture
          @email = Email.new
       end
       def sendemail
          @email = Email.new(params[:email])
          MandrillMailer.send_email(@email.to_email, @email.name, @email.subject, @email.message).deliver
       end
       def email_params
          params.require(:email).permit(:name, :email, :subject, :email)
       end
    end
  14. Since we want to submit our form data using POST method, edit your config/routes.rb file. Change get for sendemail url to post like this:
    get 'email/capture'
    
    post 'email/sendemail'
  15. Now, execute the URL:
    http://localhost:3000/email/capture

    Enter proper data and you should receive an email on the provided email address.