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.
- Let us create a new project named tableless with the command:
rails new tableless
- Next, we will generate a controller with two methods: capture and send.
rails generate controller email capture sendemail
- 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
- 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 }
- 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
- Next, we will create a model for our email. Execute the following line on command prompt:
rails generate model email
- Now add following code to the file app/models/email.rb:
-
class Email include ActiveModel::Model attr_accessor :name attr_accessor :to_email attr_accessor :subject attr_accessor :message end
- Since we don’t want to associate our model with database, we can remove the file: db/migrate/xxxxx_create_emails.rb
- 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 %>
- Next, we will edit our view file app/views/email/sendemail.html.erb to:
<h1>Your data has been submitted to your email</h1>
- 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>
- 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
- 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'
- Now, execute the URL:
http://localhost:3000/email/capture
Enter proper data and you should receive an email on the provided email address.