Many applications and websites require e-mail capture to target leads, subscribers, and customers. This is a tutorial on how to create a reusable newsletter form for e-mail and any other user information your app requires. We'll use a brand new app for the purposes of this example, but this code is extendable for most existing Rails apps as well.
Let's start by generating and migrating a model to capture the information. Feel free to use any additional database columns you may want to include here.
rails g model subscriber name:string email:string source:string rake db:migrate
Next let's create the controller with a single method, named index.
rails g controller subscribers index
The rails generator should have created a file in app/views/subscribers named index.html.erb. If it didn't work or you created the controller in your text editor, then make the file and it's corresponding folder in the command line.
mkdir app/views/subscribers touch app/views/subscribers/index.html.erb
In this new file, index.html.erb, we're going to place a form_for that will take in all of the information. Make sure to include a field for all of the database columns that you initiated during the initial model creation.
In this example, we're using and if-else statement and browser cookies: if the subscriber has already been saved, then the form won't continue to show up. Notice the email_field helper; this will throw an error if someone tries to enter an email without an "@" sign. If this is a larger app, you may choose to separate this view as a partial and add it to multiple pages.
In order to get this view to render, we'll also need to set up a route. You can just use resources to get all of the different possible subscribers routes.
# config/routes.rb resources :subscribers root 'subscribers#index'
To continue, we'll have to write some code in the controller to establish what a subscriber is. The app needs a create method to write a new subscriber object, passing through the subscriber parameters. Make sure to specify your subscriber_params in a private method. The index method below will enable the new subscriber object to be initiated on our view in app/views/subscribers/index.html.erb
In the create method, we're also adding flash messages to notify the user if their e-mail information was saved successfully or if it failed. I added these in app/views/layouts/application.html.erb with:
<% if notice %> <%= notice %> <% end %>
Finally, the model needs some validation to ensure that each subscriber fills the form out properly.
Now we're finally ready to start up the server and view our e-mail capture form in the browser. You should see something that looks like this, but if this is a production application you should add some divs and styling.
Go ahead and test the form. After you hit create subscriber, did you see the "Saved Succesfully!" notice? If not, try and de-bug the app yourself or ask a question below in the comments.
To confirm the new subscribers are being saved to the database, we can check in the rails console.
If you have any questions, fire away below or check out the repository on Github. Happy coding!