<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Colleen </title>
    <description>The latest articles on Forem by Colleen  (@leenyburger).</description>
    <link>https://forem.com/leenyburger</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F63331%2F79341857-a5e8-4b05-93d8-e5610a35d409.png</url>
      <title>Forem: Colleen </title>
      <link>https://forem.com/leenyburger</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/leenyburger"/>
    <language>en</language>
    <item>
      <title>Set a cookie read a cookie! How to (gently) collect user emails in a Rails application without Devise or requiring a password.</title>
      <dc:creator>Colleen </dc:creator>
      <pubDate>Mon, 18 Mar 2019 13:58:42 +0000</pubDate>
      <link>https://forem.com/leenyburger/set-a-cookie-read-a-cookie-how-to-gently-collect-user-emails-in-a-rails-application-without-devise-or-requiring-a-password-147j</link>
      <guid>https://forem.com/leenyburger/set-a-cookie-read-a-cookie-how-to-gently-collect-user-emails-in-a-rails-application-without-devise-or-requiring-a-password-147j</guid>
      <description>&lt;p&gt;I was asked by a client to "soft-gate" user emails. The client wanted a list of emails from people using the application, but didn't want to discourage anyone from use. This presented a problem - how could I ask users' for their emails without preventing them from using the app (or pissing them off so much they closed the window)? And how could I remember they had visited and not ask for their email address again?&lt;/p&gt;

&lt;p&gt;First things first - this solution is in no way even remotely secure. Devise has a "soft" sign up feature that allows users to start with entering only their email address (and then adding a password later) that you may want to check out if you're looking for real user authentication. In this case I was only trying to amass email addresses for a mailing list. &lt;/p&gt;

&lt;h2&gt;
  
  
  Question:
&lt;/h2&gt;

&lt;p&gt;How to collect email addresses of users without being too annoying or preventing them from using the app?&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem:
&lt;/h2&gt;

&lt;p&gt;User state must be saved somehow, because you don't want the app requesting an email address every time the user visits the site.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution:
&lt;/h2&gt;

&lt;p&gt;Save a cookie on the user's browser, and read the cookie to determine if the user has already visited the site. If they have, let them through. If they haven't, show a pop-up modal asking for their email address, but allow them to click anywhere on the page to close the modal and still use the site.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Save a cookie
&lt;/h3&gt;

&lt;p&gt;In the &lt;code&gt;create&lt;/code&gt; method in the &lt;code&gt;users&lt;/code&gt; controller add &lt;br&gt;
&lt;code&gt;cookies[:user_email] = @user.email&lt;/code&gt; if the user successfully saves. &lt;/p&gt;
&lt;h3&gt;
  
  
  Step 2: Create a modal
&lt;/h3&gt;

&lt;p&gt;I'm lazy, this is almost directly from the bootstrap docs. My modal view renders my &lt;code&gt;users#new&lt;/code&gt; form. I send in the &lt;code&gt;locals&lt;/code&gt; as &lt;code&gt;User.new&lt;/code&gt; because &lt;code&gt;form_for user&lt;/code&gt; needs a user. Usually this is automagically done for you if the form is accessed via the &lt;code&gt;create&lt;/code&gt; action.:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!-- Modal --&amp;gt;
&amp;lt;div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true"&amp;gt;
  &amp;lt;div class="modal-dialog" role="document"&amp;gt;
    &amp;lt;div class="modal-content"&amp;gt;
      &amp;lt;div class="modal-header"&amp;gt;
        &amp;lt;h5 class="modal-title" id="exampleModalLabel"&amp;gt;Modal title&amp;lt;/h5&amp;gt;
        &amp;lt;button type="button" class="close" data-dismiss="modal" aria-label="Close"&amp;gt;
          &amp;lt;span aria-hidden="true"&amp;gt;&amp;amp;times;&amp;lt;/span&amp;gt;
        &amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div class="modal-body"&amp;gt;
        &amp;lt;%= render partial: 'users/form', locals: { user: User.new } %&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div class="modal-footer"&amp;gt;
        &amp;lt;button type="button" class="btn btn-secondary" data-dismiss="modal"&amp;gt;Close&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt; 
    &amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3: Check for a cookie
&lt;/h3&gt;

&lt;p&gt;So, the bootstrap modal is designed to be triggered by a button. This is straight from the docs. I want to trigger it ONLY if the cookie isn't set. This tripped me up at first, I tried accessing &lt;code&gt;cookies[:user_email]&lt;/code&gt; in the view. No dice. The correct way:&lt;br&gt;&lt;br&gt;
In the &lt;code&gt;root&lt;/code&gt; controller index (the homepage) I added this method: &lt;br&gt;
(&lt;code&gt;the index action of the root controller&lt;/code&gt;)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  def index
    @user_email = cookies[:user_email]
  end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then in the view I checked for my cookie: &lt;br&gt;
&lt;code&gt;layouts/application.html.haml&lt;/code&gt; (or erb whatever you're into)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- if !@user_email
  #render-modal
  = render 'modals/user_email.html.erb'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So, &lt;code&gt;@user_email&lt;/code&gt; is set in the &lt;code&gt;index&lt;/code&gt; method. If &lt;code&gt;@user_email&lt;/code&gt; does not exist (i.e. the cookie has not been created), I create a &lt;code&gt;div&lt;/code&gt; with an id of &lt;code&gt;render-modal&lt;/code&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4: Show the modal
&lt;/h3&gt;

&lt;p&gt;Quick jquery snippet to show the modal if the div created above exists: &lt;br&gt;
&lt;code&gt;app/assets/javascripts/modals.js&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$(document).ready(function() {
  if ($('#render-modal').length) {
    $('#myModal').modal({show:true});
  }
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;There you have it! A quick way to render a pop-up modal if the user hasn't visited your site before. Now start collecting email addresses and sending great content to your users!&lt;/p&gt;

</description>
      <category>rails</category>
      <category>webdev</category>
      <category>modal</category>
      <category>popup</category>
    </item>
  </channel>
</rss>
