<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by brifetterley on Medium]]></title>
        <description><![CDATA[Stories by brifetterley on Medium]]></description>
        <link>https://medium.com/@brifetterley?source=rss-1c7d3eae0bd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*RE2jZ1AwIZgFb6WQfyR88g.jpeg</url>
            <title>Stories by brifetterley on Medium</title>
            <link>https://medium.com/@brifetterley?source=rss-1c7d3eae0bd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 05:12:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@brifetterley/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Create a Pop Up Modal in Vanilla JavaScript]]></title>
            <link>https://medium.com/@brifetterley/create-a-pop-up-modal-in-vanilla-javascript-9aedfb387857?source=rss-1c7d3eae0bd------2</link>
            <guid isPermaLink="false">https://medium.com/p/9aedfb387857</guid>
            <dc:creator><![CDATA[brifetterley]]></dc:creator>
            <pubDate>Mon, 09 Mar 2020 01:25:10 GMT</pubDate>
            <atom:updated>2020-03-09T01:25:10.806Z</atom:updated>
            <content:encoded><![CDATA[<h3>Step 1: Create your modal!</h3><p>You can start by creating an OuterDiv that will hold whatever elements you want inside of it.</p><pre><strong>const</strong> outerDiv = document.createElement(&#39;div&#39;);</pre><p>Create additional elements to add to your modal, for example:</p><pre><strong>const</strong> innerDiv = document.createElement(&#39;div&#39;);</pre><pre><strong>const</strong> imgDiv = document.createElement(&#39;div&#39;);</pre><pre><strong>const</strong> descriptionDiv = document.createElement(&#39;div&#39;);</pre><p>Then, don’t forget to append them!</p><pre>outerDiv.appendChild(innerDiv);</pre><pre>innerDiv.appendChild(descriptionDiv);</pre><pre>innerDiv.appendChild(amountDiv);</pre><h3>Step 2: Style your modal and set the display = ‘none’</h3><pre><strong>outerDiv.style.display = &#39;none&#39;;<br>            /* Hidden by default */</strong></pre><pre>outerDiv.style.zIndex = &#39;1000&#39;;<br>          <em> /* makes sure that your modal sits on top */</em></pre><pre>outerDiv.style.height = &#39;100%&#39;;<br><em>          /* full height */</em></pre><pre>outerDiv.style.width = &#39;100%&#39;;<br>         <em>/* full width */</em></pre><pre>outerDiv.style.position = &#39;fixed&#39;;<br>        <em>/* stays in place */</em></pre><pre>outerDiv.style.left = &#39;0&#39;;</pre><pre>outerDiv.style.top = &#39;0&#39;;</pre><pre>outerDiv.style.overflow = &#39;auto&#39;;<br>        <em>/* Enable scroll if needed */</em></pre><pre>outerDiv.style.backgroundColor = &#39;rgba(0,0,0,0.7)&#39;;<br>        <em>/* </em>semi­transparent black background that obscures the site<em>*/</em></pre><h3>Step 3: Add an EventListener to the document!</h3><p>Your event listener could be a ‘click’, ‘hover’, ‘scroll’ etc.</p><p>In my example, a pop up will appear when a user hits the bottom 10% of a webpage. I will use the scroll event!</p><pre>document.addEventListener(&#39;scroll&#39;, setModalShow)</pre><p>Typically, an Event Listener will take a <strong>callback function</strong>. In this case, the callback function ‘setModalShow’ will check if the user is at the bottom 10% of the page and, if so, <strong>set the display of the modal to ‘block’</strong> (aka — appear!)</p><pre><strong>function</strong> setModalShow() {</pre><pre>if(hitBottomTenPercent()) { <strong>outerDiv.style.display = &#39;block&#39;</strong>;}}</pre><pre><em>/* function to calculate whether at bottom 10% of page */</em></pre><pre><strong>function</strong> hitBottomTenPercent() {</pre><pre><strong>const</strong> amountScrolled = $(this).scrollTop() + $(this).innerHeight()</pre><pre>if (amountScrolled &gt;= document.body.scrollHeight * .9){</pre><pre>return amountScrolled   }}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GyA4dgX60RW_U2dOVEl5Iw.png" /></figure><p>Voila! When the user scrolls down toward the bottom, this modal will appear!</p><p>Helpful Resources:</p><p><a href="https://www.w3schools.com/howto/howto_css_modals.asp">https://www.w3schools.com/howto/howto_css_modals.asp</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9aedfb387857" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ContextAPI for State Management in React]]></title>
            <link>https://medium.com/@brifetterley/contextapi-for-state-management-in-react-c66e517c80fd?source=rss-1c7d3eae0bd------2</link>
            <guid isPermaLink="false">https://medium.com/p/c66e517c80fd</guid>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[brifetterley]]></dc:creator>
            <pubDate>Mon, 06 Jan 2020 18:36:31 GMT</pubDate>
            <atom:updated>2020-01-06T20:45:22.656Z</atom:updated>
            <content:encoded><![CDATA[<h3>Context API for State Management in React</h3><p>When learning React, one of the most tedious parts can be getting the hang of state and the passing down of props. From your parent component, you may have to send a single prop down through multiple components in order for the necessary child to have access. When it gets past 2 or 3 levels, it can become hard to keep track of where data is originating from and generally leaves margin for error.</p><p>This concept of passing down props via components is also known as <strong>Prop Drilling</strong>.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2Fa7easRPlVOXqE%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Ffail-fall-regret-a7easRPlVOXqE&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2Fa7easRPlVOXqE%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="326" frameborder="0" scrolling="no"><a href="https://medium.com/media/73e6251633750b0cef305bd5b181a0e7/href">https://medium.com/media/73e6251633750b0cef305bd5b181a0e7/href</a></iframe><p>This is where <strong>data stores </strong>can come in handy- data storing is a feature that comes with Redux and Context API, that allows you to keep your data outside of your app and inject it at any level that we want.</p><p>Today, I will focus on the steps needed to implement <strong>Context API</strong> into your app.</p><p>1. <strong>Create Context</strong></p><p>In the file where your parent state is, create a variable at the top called “AppContext” — note that the ‘App’ portion of this is replaceable with anything , but the Context is not (i.e. chickenContext) :</p><pre>const AppContext = React.createContext()</pre><p>2.<strong> Create a Provider Component</strong></p><p>This is where your data is going to live. Move your state to this component.</p><pre>class AppProvider extends Component {</pre><pre>state = {</pre><pre>name: &quot;bri&quot;</pre><pre>age: &quot;100&quot;</pre><pre>};</pre><pre>}</pre><p>3. <strong>Return the AppContext.Provider inside of the the Provider Component.</strong></p><p>The value tag below is what determines what will be injected into the child component. In this case, if we want to pass down state, then let’s set it to state!</p><pre> &lt;AppContext.Provider <strong>value = {{</strong></pre><pre><strong>state: this.state</strong></pre><pre><strong>}}</strong>&gt;</pre><pre>{this.props.children}</pre><pre>&lt;/AppContext.Provider&gt;</pre><p>4.<strong> Inside the return of your App Component, wrap your divs in an AppProvider tag</strong></p><p>Doing this will allow any child wrapped within to have access to the stored data. This means you will no longer need to pass down props when rendering out tags.</p><pre>class AppProvider extends Component {</pre><pre>render () {</pre><pre>return (</pre><pre><strong>&lt;MyProvider&gt;</strong></pre><pre>&lt;div&gt;</pre><pre>&lt;p&gt; This is the App Component &lt;/p&gt;</pre><pre>&lt;ChildComponent (NO PROPS NEEDED HERE) /&gt;</pre><pre>&lt;/div&gt;</pre><pre><strong>&lt;/MyProvider&gt;</strong></pre><pre>)}</pre><p>5. <strong>Create a Consumer Component in your chosen Child</strong></p><p>This is where you will be accessing and rendering the stored data.</p><pre>class ChildComponent extends Component {</pre><pre>render () {</pre><pre>return (</pre><pre><strong>&lt;MyContext.Consumer&gt; </strong>followed by this function:</pre><pre>{<strong>(context)</strong> =&gt; (</pre><pre>&lt;React.Fragment&gt;</pre><pre>&lt;p&gt; Im inside the {<strong>context</strong>.state.name} &lt;/p&gt;<br>&lt;p&gt; Im inside the {<strong>context</strong>.state.age} &lt;/p&gt;</pre><pre>&lt;/React.Fragment&gt;</pre><pre> )} </pre><pre><strong>&lt;/MyContext.Consumer&gt;</strong></pre><pre>)}</pre><p>The <strong>context</strong> being passed inside of the function is the value defined in step 3 , when we created the Provider Component.</p><p>Note: you can only return ONE element within the function. If you want to return more than one, you will need to use a <strong>&lt;React.Fragment&gt;</strong> tag, as shown above.</p><p>6. <strong>Updating State</strong></p><p>Okay, so now we have state being passed down — but how will we update it when we need to?</p><p>Well, you can also put helper functions inside of your context to help you keep state up to date.</p><p>Wherever you have an event listener, call the function via context, like so:</p><p>Provider Component: define the event function</p><pre>&lt;AppContext.Provider value = {{</pre><pre>state: this.state</pre><pre><strong>updateStateAge: () =&gt; this.setState({</strong></pre><pre><strong>age: this.state.age + 1</strong></pre><pre><strong>})</strong></pre><pre>}}&gt;</pre><pre>{this.props.children}</pre><pre>&lt;/AppContext.Provider&gt;</pre><p>Child Component: call the function</p><pre>&lt;button <strong>onClick = {context.updateStateAge}</strong>&gt; Click &lt;/button&gt;</pre><p>In conclusion, you need to:</p><ol><li>Create <em>Context</em></li><li>Create a <em>Provider Component</em> and define state and any callback functions</li><li>Wrap the chosen child component in a<em> Consumer Tag</em></li></ol><p>Additional information can be found here:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FXLJN4JfniH4%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DXLJN4JfniH4&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FXLJN4JfniH4%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/363de06aa19b63cb0d1936cf4c5eaf6a/href">https://medium.com/media/363de06aa19b63cb0d1936cf4c5eaf6a/href</a></iframe><p><a href="https://reactjs.org/docs/context.html">Context - React</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c66e517c80fd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using the P5.play  Library for a JS Game]]></title>
            <link>https://medium.com/@brifetterley/using-the-p5-play-library-for-a-js-game-4df01708dd81?source=rss-1c7d3eae0bd------2</link>
            <guid isPermaLink="false">https://medium.com/p/4df01708dd81</guid>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[brifetterley]]></dc:creator>
            <pubDate>Thu, 05 Dec 2019 14:20:57 GMT</pubDate>
            <atom:updated>2019-12-05T22:58:00.785Z</atom:updated>
            <content:encoded><![CDATA[<h3>Using the P5.play Library for a JS Game</h3><p>I came across the P5 Library while a partner and I were building out a falling object game in JavaScript.</p><p>It was my first time using a library, so I thought I would share the basics of what I learned this week!</p><h3>What is p5 and p5.play?</h3><p>P5.play is an extension of the P5 library, which allows you to create and utilize a canvas with objects on it.</p><p>The actual ‘game window’ with the falling object is rendered on what is known as <strong>a canvas</strong>. This canvas is a window where objects can be ‘drawn’ or rendered and animated so that they appear to move, etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LBPcOi_qoA-F9PjRGFJZzw.png" /><figcaption>click the emojis to get points</figcaption></figure><p>In our game, we had images that we wanted to fall from the top of the canvas to the bottom. The user will click on the appropriate images to accumulate points, similar to the above.</p><h3><strong>Why did we decide to use P5?</strong></h3><p>We decided that using the library would be an easier route when we ran into a problem creating <strong>Event Listeners </strong>on our objects using only the canvas and RequestAnimationFrame function. We could not find a way to attach our Event Listener, without the objects being appended to the html.</p><p>P5.play allowed us to get around this issue by providing the ability to create <strong>‘sprites’ </strong>and add ‘Event Listeners’ such as <strong>‘onMousePressed’ </strong>and <strong>‘onMouseover’</strong>.</p><p>In our case, we would use the onMousePressed function for our sprites, so that when a sprite was clicked, it would be removed and add a point to the user’s scores.</p><p>In this blog, I will write out the 3 basic steps to get you up and running on a similar P5 project.</p><ol><li><strong>Download the P5 library: </strong><a href="https://p5js.org/download/">https://p5js.org/download/</a> <strong>and the P5play library</strong>: <a href="https://molleindustria.github.io/p5.play/">https://molleindustria.github.io/p5.play/</a></li></ol><p>Add these two files to the libraries folder of your app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/312/1*dOxMZbgkZUB5ISiJWjtntw.png" /></figure><p>Last, add a &lt;script&gt; tag to the&lt;head&gt; your index.html file to include the library when your sketch loads.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1gX6_vSmG16n_qrUNImqOg.png" /></figure><p><strong>2. Write out your function setup()</strong></p><p>In this function, you create the canvas and add any elements that will go inside.</p><p><strong>function setup() {</strong></p><p>//set the dimensions of your canvas<br> createCanvas(720, 400);</p><p>//create your sprites<br>newSprite = p.createSprite(width/2, height/6)<br><strong>}</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dW1Ft4CBR2UGt0nVp1TrGw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GE0Hvvt-u_GWsLyyDaFCuA.png" /><figcaption>The sprite is the white box above.</figcaption></figure><p><strong>3. Write out your function draw()</strong></p><p>In this function, you can set the color of your canvas background and specify the location of your sprites on the canvas.</p><p><strong>function draw() {</strong></p><p>// You may want to include this clear function at the top, if you have a moving object. This will clear your sprite each time, so that a trail does not appear as it moves.</p><p><strong>clear()</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/448/1*kbY5dp1yxbh9N0EWyAcMTg.png" /><figcaption>The sprite can still be seen in it’s last position, leaving a trail.</figcaption></figure><p>// next, you should set the background color of your canvas<br><strong> background(50);</strong></p><p>// finally, draw your sprites</p><p><strong>drawSprites()</strong></p><p>After you draw your sprites, you can call the necessary functions and event listeners on them. As you can see below, for each sprite, we added a onMousePressed event listener that will remove that sprite.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/676/1*XtC7JPmYgJiQdrF7QoZ0YA.png" /></figure><p><strong>}</strong></p><p>Note: You may be wondering why my functions above have a <strong>‘p.’</strong> in front of them. This is because we decided to use the instance mode of P5.</p><p>Why was this necessary? Well, our game has 3 different levels — easy, intermediate, and hard. By keeping our p5 functions out of the global scope, we were able to more easily add in necessary modifications (such as passing in an accelerated speed) according to level.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/734/1*g4TIBNwyN2yXiXuGr8F0Zg.png" /></figure><p>As you can see, you will need to put all of your p5 functions into one big function. From there, you can pass in the argument that will need to be changed.</p><p>To learn more about the Instantiation route for p5 and what to add into your function to make it work, see below!</p><p><a href="https://p5js.org/examples/instance-mode-instantiation.html">https://p5js.org/examples/instance-mode-instantiation.html</a></p><h3>Helpful Resources</h3><p><a href="https://creative-coding.decontextualize.com/making-games-with-p5-play/">https://creative-coding.decontextualize.com/making-games-with-p5-play/</a></p><p><a href="https://molleindustria.github.io/p5.play/">https://molleindustria.github.io/p5.play/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4df01708dd81" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using a Self-Referential Model in Rails]]></title>
            <link>https://medium.com/@brifetterley/using-a-self-referential-model-in-rails-ff04dbb2ea66?source=rss-1c7d3eae0bd------2</link>
            <guid isPermaLink="false">https://medium.com/p/ff04dbb2ea66</guid>
            <category><![CDATA[ruby-on-rails]]></category>
            <dc:creator><![CDATA[brifetterley]]></dc:creator>
            <pubDate>Thu, 21 Nov 2019 14:28:39 GMT</pubDate>
            <atom:updated>2019-11-22T15:34:17.277Z</atom:updated>
            <content:encoded><![CDATA[<p>At first, the term ‘self referential model’ may sound intimidating. What is it anyway?!</p><p>As you can probably guess, a self-referential model refers to a model that can reference itself. This means that the model’s table can be a reference for <strong>ONE</strong> id in <strong>TWO</strong> different contexts. We come in contact with self-referentially modeled apps all the time.</p><p>Popular apps with this structure would be Twitter, Couchsurfing, Ebay or Airbnb.</p><p>In all of these apps, a user can play two roles. For example, on Twitter, a user can be both a<strong> follower</strong> or be<strong> followed.</strong> On Airbnb, a user can both<strong> host</strong> or be <strong>hosted</strong>. On Ebay, a user can be both a <strong>seller</strong> or a <strong>bidder</strong>.</p><p>The self-referential model exists because it allows <strong>one user </strong>to be associated with either role, when appropriate.</p><p>I came across this model when a partner and I were building out a copy of the app Couchsurfing. In our app, named ‘AstroPad’, a ‘Couchsurfer’ is referred to as a ‘padder’. Similar to a surfer on Couchsurfing, a padder could both<strong> send requests</strong> or <strong>receive requests</strong>.</p><p>This means that a padder could be both a <strong>requester </strong>and a <strong>requestee</strong>.</p><p>To accomplish this, our model looked like the below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wMvVOXt9vgYM8J18iifutg.png" /></figure><p>I know, I know, this looks like ALOT. Let me break down the key things to consider when setting up your relationships for a self-referential model.</p><ol><li><strong>What are the two roles that a user can have?</strong></li></ol><p>Is it seller and bidder, requester and requestee, customer or business owner? The possibilities are endless!</p><p>The answer to this question will inform your lines 9 and 12 from the above.</p><blockquote><strong>has_many </strong>:<strong>requestees</strong>, through: :requesting_padders, <strong>source: :requestee</strong></blockquote><blockquote><strong>has_many :requesters</strong>, through: :requested_padders,<strong> source: :requester</strong></blockquote><p>2.<strong> In what joiner model will this be important?</strong></p><p>In our case, the user could be a requster or requestee in the context of <strong>Requests</strong>. In ebay, a user could be a bidder or a seller in the context of <strong>Bids</strong>. On Twitter, a user can be followed or a follower through <strong>Relationships</strong>.</p><p>This will inform your lines 8 and 11 from above.</p><blockquote>has_many :requesting_padders, foreign_key: :requester_padder_id, <strong>class_name: ‘Request’</strong></blockquote><blockquote>has_many :requested_padders, foreign_key: :requestee_padder_id, <strong>class_name: ‘Request’</strong></blockquote><p>3. <strong>What will your foreign keys be?</strong></p><p>Given the above, the joiner models of Requests, Bids, and Relationships will hold the foreign keys.</p><p>In my Requests schema, you can see that there is a foreign key for not one ‘padder_id’ but TWO padder ids, named as <strong>requestee_padder_id</strong> and <strong>requester_padder_id</strong>. These two ids reference back to <strong>ONE padder_id</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KfyEuXHZE4vYt-QSE5r2iA.png" /></figure><p>These foreign keys will inform your lines 8 and 11 as well.</p><blockquote>has_many :requesting_padders, <strong>foreign_key: :requester_padder_id</strong>, class_name: ‘Request’</blockquote><blockquote>has_many :requested_padders,<strong> foreign_key: :requestee_padder_id</strong>, class_name: ‘Request’</blockquote><p>Hopefully, building out your model won’t be as daunting once you break it down!</p><p>Using a Self-Referential model led us to another wonderful learning journey that I wanted to touch on quickly.</p><p><strong>Nested Resources:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1frYbVQ7JeHuQgu5VII9WQ.png" /></figure><p>Due to our self-referential model, we wanted a seamless way for a user to create new requests based on the hosts page that they were on. To do this easily, we used nested routes. Why?</p><p>Essentially, the nested routes allowed us to access the requester or requestee’s padder_ids via the URL. From the URL, these ids were passed into our params, allowing us to properly associate and create new requests. Without these nested routes and therefore URLS, we would have a hard time passing in appropriate ids to our params. Doing it this way, we were able to avoid having the user explicitly state the padders id/name in a request</p><p>Nested routes can QUICKLY get out of hand, and you should avoid more than one level of nesting. It is also important to note that nested resources will also nest your params, so you will need to go one level deeper to get the keys you are looking for.</p><p>Here are some helpful resources should you ever need to use nested resources:</p><ul><li><a href="https://medium.com/@jaredrayjohnson1/4-things-i-learned-toying-around-with-nested-resources-in-rails-fed6d761e924">4 things i learned toying around with nested resources in Rails</a></li><li><a href="https://dev.to/brittanytinnin/nested-resources-in-rails-5-4oea">Nested Resources in Rails 5</a></li><li><a href="https://teamtreehouse.com/library/nested-routes-and-resources">Nested Routes and Resources</a></li></ul><p>Additional resources on Self-Referential models:</p><ul><li><a href="https://riptutorial.com/ruby-on-rails/example/10240/self-referential-association">Ruby on Rails - Self-Referential Association | ruby-on-rails Tutorial</a></li><li><a href="http://railscasts.com/episodes/163-self-referential-association">#163 Self-Referential Association</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ff04dbb2ea66" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using Devise for Authentication in Rails]]></title>
            <link>https://medium.com/@brifetterley/using-devise-for-authentication-in-rails-a1a005b6b1f4?source=rss-1c7d3eae0bd------2</link>
            <guid isPermaLink="false">https://medium.com/p/a1a005b6b1f4</guid>
            <category><![CDATA[ruby-on-rails]]></category>
            <dc:creator><![CDATA[brifetterley]]></dc:creator>
            <pubDate>Sun, 03 Nov 2019 23:44:45 GMT</pubDate>
            <atom:updated>2019-11-04T17:29:52.529Z</atom:updated>
            <content:encoded><![CDATA[<h3>Simple User Authentication for Rails Apps</h3><p>Whats the first thing you do when you download a new app? Typically, you sign up and log in!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/670/1*jvjp25kO_eOn09t_zIA9hQ.png" /></figure><p>If you are new to Rails, you might be wondering the best way to create log ins and seamlessly track new and returning users. One way to do this in rails is using a wonderful gem called<strong> devise</strong>.</p><p>This gem allows you to generate logins and store passwords for authentication using your User model.</p><p>In this blog, I will give you 6 simple steps for integrating <strong>devise</strong> into your app. Using this, you will have user authentication set up in just a few minutes!</p><ol><li>First, add your gem to your <em>Gemfile.rb</em>.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L-KeaeL0zfMGwIkIZcyL1A.png" /></figure><p>and dont forget to:</p><pre>bundle install</pre><p>2. After your gem is installed, run:</p><pre>rails generate devise install</pre><p>You should see some instructions in your terminal for setup:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bRPK1fY-ekzP3yrTBm8Z0g.png" /></figure><p>Add your default url path in your <em>config/environments/development.rb file</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1zoNoYcGzxtqLK3_ClV0KQ.png" /></figure><p>Typically, that URL would look like this:</p><pre>config.action_mailer.default_url_options = { host: ‘localhost’, port: 3000 }</pre><p>Be sure to follow steps 2–4 as well. This sets up a view file for you and connects your controller methods to their routes.</p><p>4. Ok, so now you have your devise gem set up. Woo hoo!</p><p>Now, lets hook it up to the appropriate model. In most use cases, this would be the <strong>User </strong>model, but it could also be assigned to an Admin model or any other, if needed.</p><p>In terminal, run:</p><pre>rails generate devise User</pre><p>This command should add a <em>user.rb </em>file to your <em>models folder</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/426/1*tgNeVwdGX8rGITWUV7ctkQ.png" /></figure><p>Dont forget to:</p><pre>rake db:migrate</pre><p>Once that is done, check your <em>schema</em> to see what your new user table looks like! You should have a bunch of fields like so:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*foHb6iNML27EYM9Nvgz2vA.png" /><figcaption>Looking good!</figcaption></figure><p>5. Last, utilize the built in helper methods in your <em>UsersController</em> file (that you will need to create!)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aTZ1T9GZWZ1B8GV2JWKWbg.png" /></figure><p>If you run <strong>rake routes</strong>, you will see all of the new routes available to you</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_ZmcA38cOXP-jY-G5BC2-Q.png" /></figure><p>6. Let’s test it out! Run <strong><em>rails s</em></strong> and get your server going.</p><p>Visit <a href="http://localhost:3000/users/sign_up">http://localhost:3000/users/sign_up</a> and voila! You should see a simple sign up/log in page rendered for you.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/762/1*0njfq9wrYyIsOUnlDtuwIw.png" /></figure><p>Play around with the different pages and make changes as you like. You have the ability to set different password character minimums and action permissions.</p><p>Devise is also handy when you want to allow users the ability to only edit their own posts and comments.</p><p>If you run into any issues, be sure to check out the great documentation on github <a href="https://github.com/plataformatec/devise"><strong>here</strong></a>!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a1a005b6b1f4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>