<?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 Stacey McKnight on Medium]]></title>
        <description><![CDATA[Stories by Stacey McKnight on Medium]]></description>
        <link>https://medium.com/@staceymck?source=rss-38e949a44cff------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*m1-_g4brsLHPa74MtvvLkg.jpeg</url>
            <title>Stories by Stacey McKnight on Medium</title>
            <link>https://medium.com/@staceymck?source=rss-38e949a44cff------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 22:41:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@staceymck/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[A simple public speaking template for developers]]></title>
            <link>https://staceymck.medium.com/a-simple-public-speaking-template-for-developers-258699825291?source=rss-38e949a44cff------2</link>
            <guid isPermaLink="false">https://medium.com/p/258699825291</guid>
            <category><![CDATA[communication]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[public-speaking]]></category>
            <category><![CDATA[developer]]></category>
            <dc:creator><![CDATA[Stacey McKnight]]></dc:creator>
            <pubDate>Tue, 10 Jan 2023 03:36:39 GMT</pubDate>
            <atom:updated>2023-01-10T23:24:40.473Z</atom:updated>
            <content:encoded><![CDATA[<h4>3 ways comms skills can make you a better dev — Part III</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*mjwrXv67k_AxMJET" /><figcaption>Photo by <a href="https://unsplash.com/@kanereinholdtsen?utm_source=medium&amp;utm_medium=referral">Kane Reinholdtsen</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>I do as much — maybe more — public speaking in my role as a software developer as I did in my communications roles.</p><p>This partly stems from the nature of my current role as a team lead, but it’s also typically part of being a developer.</p><p>Most devs have to regularly speak in front of peers during events like stand-ups, kick-off meetings, and retros.</p><p>As I’ve worked on more structured talks, like presentations and even short-form ‘lightning’ talks, I’ve found myself thinking back to a public speaking course I took as a comms major that taught me to apply a specific formula when preparing a speech.</p><p>There are lots of ways to approach prepared talks. But for me, having a starting structure removes the daunting feeling of staring at a blank page. When I have a presentation coming up, I usually set up a doc with the following sections, and then start filling them in:</p><p>Introduction:</p><ul><li>What are you going to talk about and why is it relevant to the audience?</li><li>Preview the main points / thesis</li></ul><p>Main points:</p><ul><li>Headline the main idea followed by supporting details</li><li>Add a transition between each main sections (simple format — we just talked about X, now we’ll explore Y)</li></ul><p>Conclusion:</p><ul><li>Recap the thesis, review the main points + relevance to listeners</li><li>Wrap with a memorable ending (tying back to intro is often a decent way to wrap up)</li></ul><p>I used this general format to prepare the outline for a talk I gave at RubyConf in 2022. At 30 minutes, it was longer than any presentation I’d worked on for a while. The template provided a foundation to get started and resources like <a href="https://medium.com/the-art-of-keynoting/the-20-minute-rule-for-great-public-speaking-on-attention-spans-and-keeping-focus-7370cf06b636">this post on breaking up longer talks</a> into smaller chunks helped me fine tune the flow.</p><p>Spending less time thinking about the overall flow and structure for each presentation frees me up to spend more time on the part that does change significantly with each presentation — the content.</p><h4>Pick up more communication tips for devs:</h4><ul><li><a href="https://staceymck.medium.com/3-ways-comms-skills-can-make-you-a-better-developer-a31664354826">Intro: 3 ways comms skills can make you a better developer</a></li><li><a href="https://staceymck.medium.com/write-pull-requests-that-invite-reviews-84955bd2e58a">Part I: Write pull requests that invite reviews</a></li><li><a href="https://staceymck.medium.com/think-like-a-copy-editor-to-move-faster-as-a-developer-72526f6c1946">Part II: Think like a copy editor to move faster as a developer</a></li><li><em>Part III: A simple public speaking template for developers</em></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=258699825291" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Think like a copy editor to move faster as a developer]]></title>
            <link>https://staceymck.medium.com/think-like-a-copy-editor-to-move-faster-as-a-developer-72526f6c1946?source=rss-38e949a44cff------2</link>
            <guid isPermaLink="false">https://medium.com/p/72526f6c1946</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[communication-skills]]></category>
            <category><![CDATA[developer]]></category>
            <category><![CDATA[communication]]></category>
            <category><![CDATA[development]]></category>
            <dc:creator><![CDATA[Stacey McKnight]]></dc:creator>
            <pubDate>Tue, 10 Jan 2023 03:35:19 GMT</pubDate>
            <atom:updated>2023-02-06T23:58:11.652Z</atom:updated>
            <content:encoded><![CDATA[<h4>3 ways comms skills can make you a better dev — Part II</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MPUVk7FEEDjm_Yz8b5UugA.png" /><figcaption>Photo by <a href="https://unsplash.com/@towfiqu999999?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Towfiqu barbhuiya</a> on <a href="https://unsplash.com/photos/oZuBNC-6E2s?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p><strong>“The user has to have the right permissions to access the page or they’re redirected”</strong></p><p>This might seem like a simple statement, but it invites questions: What permissions? What page? Where are they redirected?</p><p>When communicating with teammates, it’s easy to assume they’ll understand what you mean without much trouble because you share the same general context. I’ve shot off quick, ambiguous questions like “Hey, does this function make sense?”, thinking I’m saving time by writing a short question.</p><p>But I’ve found that unclear language usually slows things down. It can cause you to spend more time asking follow-up questions, providing needed context, or worse, mistakenly focusing time and effort on something that isn’t relevant.</p><p>Copy editing is all about being both concise and precise. The meaning of a sentence can sometimes change simply by moving a comma. Approaching communication like a copy editor asks you to look for ways to remove ambiguity while omitting unneeded words so comms stay efficient.</p><p>Using precise language comes in handy across the board, from listing the steps to replicate a bug to understanding business requirements when getting started on a new piece of work.</p><p>Being explicit is especially useful when asking for help getting unstuck. To ask clearer questions, try one or more of the following:</p><ul><li>Replace terms that could have multiple interpretations with explicit language: “The function isn’t working” becomes “some_function on line 17 returns X when I expect it to return Y”</li><li>Provide a simple example to illustrate intended behavior (particularly helpful when multiple factors can impact an outcome): If a user does X when Y condition is met, then Z should happen</li><li>Consider any follow-up questions the other person might have and address them in the initial question: “What have you tried already?”</li></ul><p>If you’re interested in more ways to make the most of your questions — especially in a pair programming context— check out <a href="https://youtu.be/FIvx-SCzaJQ">Kelly Ryan’s talk from RubyConf 2022</a>. She provides great suggestions for both the person requesting to pair and the person providing assistance.</p><h4>Pick up more communication tips for devs:</h4><ul><li><a href="https://staceymck.medium.com/3-ways-comms-skills-can-make-you-a-better-developer-a31664354826">Intro: 3 ways comms skills can make you a better developer</a></li><li><a href="https://staceymck.medium.com/write-pull-requests-that-invite-reviews-84955bd2e58a">Part I: Write pull requests that invite reviews</a></li><li><em>Part II: Think like a copy editor to move faster as a developer</em></li><li><a href="https://medium.com/@staceymck/a-simple-public-speaking-template-for-developers-258699825291">Part III: A simple public speaking template for developers</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=72526f6c1946" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Write pull requests that invite reviews]]></title>
            <link>https://staceymck.medium.com/write-pull-requests-that-invite-reviews-84955bd2e58a?source=rss-38e949a44cff------2</link>
            <guid isPermaLink="false">https://medium.com/p/84955bd2e58a</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[communication]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[communication-skills]]></category>
            <category><![CDATA[developer]]></category>
            <dc:creator><![CDATA[Stacey McKnight]]></dc:creator>
            <pubDate>Tue, 10 Jan 2023 03:33:09 GMT</pubDate>
            <atom:updated>2023-01-10T03:37:19.979Z</atom:updated>
            <content:encoded><![CDATA[<h4>3 ways comms skills can make you a better dev — Part I</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sCk1UXsDqwmF1c4fqD26pQ.png" /><figcaption>Photo by <a href="https://unsplash.com/@repules?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Márton Kopasz</a> on <a href="https://unsplash.com/photos/atUOOWEolqY?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a> (inverted)</figcaption></figure><p><strong>Don’t bury the lead.</strong></p><p>I picked up this journalism mantra as a communications major, and it’s stuck with me as I moved from writing press releases to pull requests.</p><p>What’s the lead? And what does it have to do with pull requests?</p><p>If you examine news stories, you’ll find they commonly follow a structure called the <a href="https://www.nngroup.com/articles/inverted-pyramid/">inverted pyramid</a>. They start with a lead paragraph that contains the most important and newsworthy information — the base of the pyramid — followed by supporting paragraphs with less essential details.</p><p>The stories are written this way to help the reader quickly understand the gist of the overall article and what types of details will follow the intro. If the reader only makes it through the first paragraph, they’ll still have learned key details like the who, what, where, when, and why.</p><p>Applying the inverted pyramid style to the body of a pull request can similarly benefit your teammates during code review.</p><p>I can categorize most PR bodies I’ve written or read into the following buckets:</p><ol><li>Too much info</li><li>Too little info</li><li>Just enough info</li></ol><p>I’ve found that the inverted pyramid makes it easier to write a PR body with just enough info.</p><p>The body opens with a clear, concise description of the problem and solution followed by supporting details like:</p><ul><li>The reasoning behind the solution</li><li>Relevant business logic</li><li>Areas where you especially want feedback</li><li>QA steps</li><li>Links to related tickets, requirement docs, etc.</li></ul><p>(Bullet points can also be a great tool for making key details easy to find)</p><p>Some teams use templates for PR bodies, but if you have freedom in any of the sections and find yourself writing a paragraph or more, think about the inverted pyramid. Its formulaic nature speeds up the writing process while helping you get your point across.</p><h4>Pick up more communication tips for devs:</h4><ul><li><a href="https://staceymck.medium.com/3-ways-comms-skills-can-make-you-a-better-developer-a31664354826">Intro: 3 ways comms skills can make you a better developer</a></li><li><em>Part I: Write pull requests that invite reviews</em></li><li><a href="https://staceymck.medium.com/think-like-a-copy-editor-to-move-faster-as-a-developer-72526f6c1946">Part II: Think like a copy editor to move faster as a developer</a></li><li><a href="https://medium.com/@staceymck/a-simple-public-speaking-template-for-developers-258699825291">Part III: A simple public speaking template for developers</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=84955bd2e58a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[3 ways comms skills can make you a better developer]]></title>
            <link>https://staceymck.medium.com/3-ways-comms-skills-can-make-you-a-better-developer-a31664354826?source=rss-38e949a44cff------2</link>
            <guid isPermaLink="false">https://medium.com/p/a31664354826</guid>
            <category><![CDATA[communication-skills]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[communication]]></category>
            <category><![CDATA[developer]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Stacey McKnight]]></dc:creator>
            <pubDate>Tue, 10 Jan 2023 03:31:45 GMT</pubDate>
            <atom:updated>2023-01-10T03:45:28.824Z</atom:updated>
            <content:encoded><![CDATA[<h4>Tips from a dev with a communications background</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4PpFpAGLUPkMthXKrd8Ycg.png" /></figure><p>When I transitioned from a career in nonprofit communications to software development, I expected the nature of my work would significantly shift. While the types of problems I’m solving have changed, I’ve found that comms skills continue to play a central role in my work as a developer.</p><p>In this series, I’ll share a few tools from my comms background that help in my day-to-day dev work with the hope that they can help you as well.</p><p>One of the things I love most about the dev community is its focus on sharing knowledge — from how things work and tangly problems we’re facing to ideas that excite us.</p><p>Being able to communicate information clearly and efficiently pushes us forward together, and techniques pulled from the comms field can help:</p><ul><li>Part I: The inverted pyramid style of writing can highlight key info in PR bodies — <a href="https://staceymck.medium.com/write-pull-requests-that-invite-reviews-84955bd2e58a">Read more…</a></li><li>Part II: Thinking like a copy editor can help us avoid getting slowed down by ambiguity and miscommunication — <a href="https://staceymck.medium.com/think-like-a-copy-editor-to-move-faster-as-a-developer-72526f6c1946">Read more…</a></li><li>Part III: Using a simple template for presentations can save time and provide direction when public speaking needs arise — <a href="https://medium.com/@staceymck/a-simple-public-speaking-template-for-developers-258699825291">Read more…</a></li></ul><p>If you have any communication tips of your own, I’d love to chat in the comments 💬</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a31664354826" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[5 free design tools for your bootcamp projects]]></title>
            <link>https://medium.com/design-bootcamp/5-free-design-tools-for-your-bootcamp-projects-c1ecb5cc0f6c?source=rss-38e949a44cff------2</link>
            <guid isPermaLink="false">https://medium.com/p/c1ecb5cc0f6c</guid>
            <category><![CDATA[css]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[design]]></category>
            <category><![CDATA[bootcamp]]></category>
            <dc:creator><![CDATA[Stacey McKnight]]></dc:creator>
            <pubDate>Mon, 19 Jul 2021 15:52:40 GMT</pubDate>
            <atom:updated>2021-07-20T02:02:15.069Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AIwDN_0Kn5d8ewDEsGwCbw.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@nordwood?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">NordWood Themes</a> on <a href="https://unsplash.com/s/photos/design?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>I’ve heard programming bootcamp alums describe their learning experience as drinking from a fire hydrant. Or running a marathon. My own experience has felt like both.</p><p>Last year I jumpstarted a career transition from marketing and graphic design work to full stack development via a 5-month training program through<a href="https://flatironschool.com/"> Flatiron School</a>. The program’s fast pace, especially during weeks focused on project builds, made it important to work efficiently and find a balance between learning a wide range of concepts (breadth) and diving deep into core concepts with the goal of mastery (depth).</p><p>The core curriculum in my program centered on programming fundamentals along with a set of languages and frameworks for building feature-rich sites. Building visually appealing apps was encouraged, but generally as a stretch goal if you had extra time to explore design concepts.</p><p>I came into bootcamp with graphic design and CSS experience, but not everyone does — and finding extra time during bootcamp to learn and apply design principles can be challenging.</p><p>Thankfully there are great tools that can <strong>speed up the design process</strong> while helping you make informed design decisions. In this post, we’ll cover 5 types of design tools I’ve found helpful for development projects, which I hope also prove useful for you.</p><h3><strong>1. Wireframing + mockups</strong></h3><p>When starting a new project, I quickly sketch out wireframes to test different layouts until settling on a firm plan for each page template. Experimenting with layouts through quick, rough sketches is faster than experimenting by rearranging code snippets after you’ve already started building.</p><p>Sketches with low levels of detail can then be converted into digital mockups with high levels of detail via user interface (UI) design programs like <a href="https://www.figma.com/">Figma</a> and <a href="https://www.sketch.com/">Sketch</a>. Like sketching, creating mockups lets you experiment with design and establish a clear goal before you start coding. It can also help you recognize opportunities to reuse code for similar elements.</p><p>I’ve been using Figma, which includes a free tier and is pretty friendly for new users. You can create layouts for various screen sizes and add vectors, text, and images. To easily change elements of your design, you can also create reusable design components. Updating the original component applies the change to all instances/copies of that component.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C0jMI5Ob8YqSTcxtDEBAEw.png" /><figcaption>Figma lets you create ‘frames’ based on various device sizes. You can then add images, text, and vectors to create mockups like these I created when planning my portfolio site.</figcaption></figure><p>To dive deeper, you can check out tutorials on Figma’s <a href="https://www.youtube.com/channel/UCQsVmhSa4X-G3lHlUtejzLA">YouTube channel.</a></p><h3><strong>2. Typography Scales</strong></h3><p>When considering typography, you’ll need to make decisions about sizing. Most sites use a variety of text sizes to create a visual hierarchy on the page, but how do you decide what size to make your headings and body text so that the sizes feel harmonious rather than haphazard? <a href="https://spencermortensen.com/articles/typographic-scale/">Typographic scales</a> provide a solution.</p><p>Similar to a musical scale, a typographic scale is a progression of text sizes based on a numerical ratio that produces harmony between the sizes. No guesswork needed. You can explore type scales for your next project through tools like <a href="https://type-scale.com/">type-scale.com</a> and <a href="https://www.modularscale.com/">modularscale.com</a>.</p><h3><strong>3. Color Pickers</strong></h3><p>Along with type harmony, considering color harmony can also improve your design. Some colors just clash while others pair well together, such as colors that are directly across from each other on the color wheel (complementary colors) or side by side on the color wheel (analogous colors).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PzoMCDmj6xO0bTAZ1Oscng.png" /></figure><p>To find colors for your site that harmonize, you can use a tool like <a href="https://www.canva.com/colors/color-wheel/">Canva’s color wheel</a> or <a href="https://colorpicker.me/">ColorPicker.me</a>.</p><h3><strong>4. CSS generators</strong></h3><p>If CSS isn’t your jam, you can utilize <a href="https://cssgenerator.org/">CSS generators</a> to create the CSS needed for design elements like borders, shadows, and gradients. Generator sites let you select a property or element to work with, such as a button, and then use sliders, color pickers, and other controls to manipulate the element until it fits your design goals.</p><p>As the name implies, the generator outputs the CSS code needed to achieve your customized look. Beyond generators focused on individual elements like buttons, you can also find <a href="https://flexgenerator.com/">flexbox</a> and <a href="https://grid.layoutit.com/">grid</a> generators, which can help you structure larger sections of your app.</p><h3><strong>5: SVG generators</strong></h3><p>You can also find tools that generate SVGs for use as section dividers, backgrounds, or other design elements. While planning my portfolio site, I tested out two user-friendly generators, <a href="https://haikei.app/">Haikei</a> and <a href="https://www.shapedivider.app/">Shape Divider</a>. Both present a set of property controls, like color and size, that you can adjust to customize your SVG shape. Once happy with the look, you can download/copy your SVG’s code and related CSS styling to use in your app.</p><h3><strong>Share your recommendations</strong></h3><p>Design matters. Good design makes it easier for users to achieve their goals, leading to better user experiences. If you’re new to the development field, it can also help your portfolio projects stand out as you head into the job search.</p><p>While you might not have started your coding journey with design experience, tools like the ones we’ve covered here can help you add polish to your sites and boost your productivity when it comes to design.</p><p>If there are other design tools you’d recommend, please share in the comments!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c1ecb5cc0f6c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/design-bootcamp/5-free-design-tools-for-your-bootcamp-projects-c1ecb5cc0f6c">5 free design tools for your bootcamp projects</a> was originally published in <a href="https://medium.com/design-bootcamp">Bootcamp</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[4 Tips for your first hackathon]]></title>
            <link>https://medium.com/geekculture/4-tips-for-your-first-hackathon-f9cbbc9c4096?source=rss-38e949a44cff------2</link>
            <guid isPermaLink="false">https://medium.com/p/f9cbbc9c4096</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[flatiron-school]]></category>
            <category><![CDATA[hackathons]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[teamwork]]></category>
            <dc:creator><![CDATA[Stacey McKnight]]></dc:creator>
            <pubDate>Tue, 15 Jun 2021 19:11:06 GMT</pubDate>
            <atom:updated>2025-01-23T15:19:31.659Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EUP46PS-9ATb8Dq_HZQcLQ.png" /><figcaption>Login page created by the team during my first hackathon</figcaption></figure><p>After changing careers from marketing to software engineering, I didn’t anticipate that my experience writing press releases would come in handy as a web developer. But it did this weekend as my hackathon teammates and I kicked off a 3-day coding challenge by writing a mock press release announcing the app we planned to build.</p><p>No one on our team had participated in a hackathon before, so as we got started we didn’t really know what to expect beyond a weekend of fast-paced teamwork. I certainly didn’t expect press releases. (We’ll get to the purpose of the press release in a bit.)</p><p>If you’re thinking about signing up for a hackathon or are getting ready for your first one, I hope the takeaways from my experience can help you know what to expect and how to get the most out of a high-energy weekend of collaboration and coding.</p><h3>Setting the stage</h3><p>Before we jump into the tips, let me provide you with the context. The hackathon I participated in was a remote event geared toward current and recent graduates of <a href="https://flatironschool.com/">Flatiron School’s</a> data science, cybersecurity, and software engineering programs. Teams were encouraged to have 5–6 members and were tasked with creating an application around the theme of working together.</p><p>Leading up to the event, I worked on a pitch with a fellow software engineering grad so we could start recruiting more teammates. With the recent shift toward more remote work, we planned to build an app to help dispersed team members get to know each other on a more personal level with the end goal of improving collaboration. We connected with four other developers to form a team and hopped on a Zoom call as the hackathon started to introduce ourselves and outline what we planned to create together. Now for the tips.</p><h3>1: Spend time talking about team logistics, not just product details</h3><p>As we introduced ourselves, the need to coordinate team logistics became clear. We were spread over multiple time zones, with one team member located in Hungary and the rest scattered throughout the US.</p><p>Whether your team will be remote or all in the same room, talking through logistics in addition to product goals upfront will save you time. It can be helpful to write down questions in advance to guide the discussion, for example:</p><h4><strong>Communication and roles</strong></h4><ul><li>What communication platforms will we use? (Zoom, Slack, Discord, etc.)</li><li>How often will we meet as a group and when?</li><li>What tools will we use for task management? (Asana, Trello, etc.)</li><li>Who will be responsible for which tasks?</li><li>Will we mostly work independently, in pairs, sub-teams, etc.?</li></ul><blockquote><strong>Quick tip:</strong> If working remotely, have a backup way to contact your team members. The Flatiron event was hosted on Discord, but service went down for some users the first day. Thankfully Slack and a team Zoom space provided a way to keep in touch until service was back.</blockquote><h4><strong>Technology</strong></h4><ul><li>What tech stack will we use?</li><li>If using a framework like React, will we use hooks or classes? NPM or Yarn?</li><li>Will we use custom CSS/Sass styling or a system like MaterialUI?</li><li>Who will set up the shared repos?</li><li>Who will make decisions about merging in pull requests?</li></ul><h4><strong>Product details</strong></h4><p>Remember that press release from earlier? The hackathon organizers recommended writing one the first day because the process requires thinking through core product questions like:</p><ul><li>What problem will our app address?</li><li>How will our app provide a solution and what features are involved?</li><li>Who are our target users and how will they benefit from using the app?</li></ul><p>Talking through these questions helped us solidify our product goal and agree on a minimum viable product (MVP), which served as a guide for prioritizing tasks. Our team separated what was required for a prototype vs what was a stretch goal then broke down the required features into discrete tasks and assigned them to team members using <a href="https://app.asana.com/">Asana.</a></p><h3>2: Anticipate bugs and troubleshooting</h3><p>Working with new people on a new project means you’ll likely run into errors and other tech-related challenges. Our frontend team worked with React and often ran into merge conflicts related to the package.lock.json file when pulling down the most recent version of the repo from GitHub.</p><blockquote><strong>Quick tip:</strong> If you run into this issue yourself, the <a href="https://docs.npmjs.com/cli/v7/configuring-npm/package-locks#resolving-lockfile-conflicts">NPM docs</a> state that from version 5.7.0 forward, you can resolve such conflicts by manually fixing them in the package.json file and then running npm install --package-lock-only.</blockquote><p>Keeping communication channels open made it easy to help each other out when we ran into issues. We often teamed up to troubleshoot via screen sharing, but at times it was more helpful for one person to tackle a bug so the team could keep making progress on other parts of the app.</p><h3>3: Manage your time and build in breaks</h3><h4><strong>Meal breaks</strong></h4><p>The hackathon felt a bit like a time warp as our team communicated and coded from early mornings into late nights. Once you get caught up in the whirlwind of activity, it’s easy to lose track of time, so consider building in breaks (especially meal breaks) as a team.</p><h4><strong>Presentation prep</strong></h4><p>At the end of your hackathon, you’ll likely present your prototype to other attendees and event coordinators. As you consider what your team can realistically build during the event, make sure to allot time for preparing your presentation and doing at least one practice run together.</p><h4><strong>Progress check-ins</strong></h4><p>Tasks can take longer or shorter than expected. Keeping each other in the loop on progress allows the team to more effectively identify next steps since priorities can shift based on the time remaining.</p><h3>4: Know your goals for the event</h3><p>While the main goal of a hackathon is to quickly prototype a project, thinking through additional goals you have for participating can help guide your decisions and interactions. If it’s your first hackathon or experience coding with a new group of people, one of your primary goals might be as simple as strengthening your team collaboration skills.</p><h4><strong>Potential goals could include:</strong></h4><ul><li>Building positive new connections in the field</li><li>Learning a new piece of technology</li><li>Practicing development in a fast-paced environment</li><li>Adding to your resume</li><li>Gaining a better understanding of how other people approach problem-solving</li></ul><p>To keep my own goals in mind, I wrote down my top three on a notecard to keep on my desk as a reminder of how I wanted to approach the weekend.</p><h3>Keep collaborating</h3><p>My first hackathon was an incredibly fun experience of teamwork and learning, and I would recommend an event like this to anyone interested in developing your creativity and collaboration skills. I hope these takeaways help you and your team as you head into your own hackathon.</p><p>In the spirit of collaboration, feel free to share your own hackathon tips in the comments!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f9cbbc9c4096" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/4-tips-for-your-first-hackathon-f9cbbc9c4096">4 Tips for your first hackathon</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Customizing React Color’s Sketch Picker to display recently used colors]]></title>
            <link>https://staceymck.medium.com/customizing-react-colors-sketch-picker-to-display-recently-used-colors-b1eb09724ed6?source=rss-38e949a44cff------2</link>
            <guid isPermaLink="false">https://medium.com/p/b1eb09724ed6</guid>
            <category><![CDATA[color-picker]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[react-color]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Stacey McKnight]]></dc:creator>
            <pubDate>Mon, 10 May 2021 00:13:01 GMT</pubDate>
            <atom:updated>2021-05-10T00:13:01.943Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Square composed of 100+ triangles, partly colored in shades of teal beside a color selector tool with a hue slider and 15 swatches of colors." src="https://cdn-images-1.medium.com/max/1024/1*5q2SX5ESJOO1OimE9-g6bQ.png" /></figure><p>Whenever I gather materials to start a new painting, I always reach for a palette to hold the paints I’ll be using. Most often it’s a well-used piece of palette paper covered with the last painting’s color scheme. The palette makes it easy to switch between core colors without having to remix a frequently used color for each new brush stroke.</p><p>For a recent art-themed React project, I wanted to emulate the benefits of a palette and allow users to easily access their recently used colors as they ‘painted’ a digital canvas. In this post, we’ll build out this functionality by customizing <a href="https://casesandberg.github.io/react-color/">React Color’s</a> SketchPicker component. We’ll also explore other customizations available through the SketchPicker API that make the component a flexible and helpful tool.</p><p>You can check out the frontend repo for the project this post is based on <a href="https://github.com/staceymck/collab-color">here</a> if you’d like more context. Code samples in this post have been simplified so we can focus on our color picker customization.</p><h3>Setting up SketchPicker with Core Props</h3><p>React Color is a package you can install through <a href="https://www.npmjs.com/package/react-color">npm</a> that gives you access to 13 different color picker components for use in React, ranging from sliders and swatches to input fields. You can also use helper components to create your own picker if desired.</p><p>For this demo, we’ll work with the pre-built SketchPicker<strong> </strong>component<strong>, </strong>so let’s install the package in our app’s directory with the command npm install react-color — save.</p><p>Next we’ll import the SketchPicker into a parent component in our app called StudioPage, which will also render the Canvas component our users will be painting. We’ll also set up local state in the parent component to manage the active color on the color picker.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6daa362845b97a41ef5daf4e1dbdf0f9/href">https://medium.com/media/6daa362845b97a41ef5daf4e1dbdf0f9/href</a></iframe><p>You’ll notice that SketchPicker is receiving two props from the parent component. Each React Color picker is prepared to receive at least two props, color and a choice of either onChange or onChangeComplete<strong>.</strong></p><p>As the <a href="https://casesandberg.github.io/react-color/">React Color docs</a> explain, color<strong> </strong>controls the color picker’s active color. In the code snippet above, we’ve set an activeColor value in local state that will display as the selected color when the SketchPicker loads. For your color prop, you can pass a hex code as a string or an object containing either RGB or HSL values. In this demo, we’ll work with hex colors.</p><p>Since the color picker’s selected/active color is controlled by the value of activeColor in our parent component’s state, we need a way to update activeColor each time a user selects a new color on the SketchPicker.</p><p>To achieve this, React Color lets you pass the color picker a function to execute when a color change occurs. If you want the function to fire on <em>every </em>change (including drag events), pass your callback function via a prop called onChange. If you want it to execute only <em>once </em>per core change, then pass your callback to onChangeComplete.</p><p>Note that if you use onChangeComplete, you can drag your color selector (via the hue slider or saturation block) to a new position, but the selector will not move with your cursor as you drag it. It remains stationary until you release the drag motion. Then the selector will jump to the new position. The selector <em>does</em> follow your pointer in a visual dragging motion if you use onChange.</p><p>We’ll name our callback function handleChangeComplete. Whatever callback you pass to onChange or onChange complete can receive both the selected color and the event as arguments. For this demo, we only need color details, so we’ll just pass in color to our callback.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/51682beb9311530d4868b7438d0debfc/href">https://medium.com/media/51682beb9311530d4868b7438d0debfc/href</a></iframe><p>Inside this callback, we’ll update the value of activeColor in our local state and set it equal to color’s hex value by calling color.hex. You can also access the RGB or HSL values of the selected color via color.rgb or color.hsl. Hex values can come through with lower or uppercase letters, so for consistency, we’ll save the values in uppercase format. This becomes important later when we build out our recent colors tracking feature.</p><h3>Customizing the Color Picker</h3><p>Now that we have the base functionality setup, let’s explore ways to customize the SketchPicker.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0a4546a9192b0b162d58d488d6f477d7/href">https://medium.com/media/0a4546a9192b0b162d58d488d6f477d7/href</a></iframe><p>Some of the color picker options come with their own APIs, which are outlined in the <a href="https://casesandberg.github.io/react-color/#api-individual">React Color docs.</a> SketchPicker lets you pass additional props if desired, including:</p><p><strong>disableAlpha<br></strong>Alpha describes the opacity of a color. If you don’t want to display alpha values on the color picker, pass disableAlpha the boolean value true (set to false by default). Make sure to pass true as a boolean value, not a string. Since opacity did not factor into the app I was working on, I chose to disable this feature.</p><p><strong>width<br></strong>The SketchPicker’s width is set to 200px by default, but you can pass in a new value to change the width. I chose to use a percentage so the picker would scale with its container.</p><p><strong>className<br></strong>If you want to further adjust the component’s style, use the className prop to pass in your own styles. Since we don’t have access to the picker’s styles, you might need to <a href="https://css-tricks.com/override-inline-styles-with-css/">override them in your CSS</a> if you want to change already set values.</p><p><strong>onSwatchHover<br></strong>You can customize the hover behavior of the SketchPicker’s preset color swatches. Add an onSwatchHover prop to your picker and pass it a callback function that executes your desired hover action. The callback function will be able to receive both color (i.e. the selected color) and event as arguments.</p><p><strong>presetColors<br></strong>The SketchPicker displays 15 color swatches by default, but you can pass your own set of colors as either hex codes or RGB/HSL objects. You can also pass in more or less than 15 options. We’ll use this prop to transform the color swatches section into our own paint palette that tracks recently used colors.</p><h3>Tracking Recently Used Colors in our Picker</h3><p>Returning to our original goal, if a user works with a custom color pulled from the slider or saturation block, we want to keep track of that color so they can easily return to it later and not have to place their slider/selector in the exact same location. It’s hard, I’ve tried it.</p><p>In the code sample above, you may have noticed we’re passing<strong> </strong>this.state.presetColors as the presetColors prop to our color picker.</p><p>In our parent component’s local state, we’ll add presetColors and set it equal to the default presetColor values provided by the SketchPicker.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/02c87f2b05b7b17e384d24b28b397efe/href">https://medium.com/media/02c87f2b05b7b17e384d24b28b397efe/href</a></iframe><p>These 15 default colors provide a nice starting palette, but we’ll update this slice of state with each new color used so that it becomes a rotating color palette displaying recently used colors.</p><p>In the app I worked on, a user can click on shapes within an SVG, our &lt;Canvas /&gt;<strong> </strong>component, to add colors. Each click triggers a callback action, handleClick, in StudioPage<strong> </strong>that accepts the shape’s id and the currently active color. It then dispatches an action via a prop called addColor<strong> </strong>that updates the shape’s color. Understanding this user interaction is relevant because it helps determine when a color becomes ‘recently used’. In this case, we can say a color becomes ‘recently used’ when a user adds it to a canvas shape, or in other words, when a click event happens on the canvas.</p><p>Now let’s take a look at the handleClick function we’re passing to our Canvas component.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/33ce2619cc70f2d0cfb94644d7e526d5/href">https://medium.com/media/33ce2619cc70f2d0cfb94644d7e526d5/href</a></iframe><p>As mentioned above, this function accepts a shape/polygon id and the active color from our color picker. The first line handles updating our canvas and isn’t our focus here. Instead, let’s walk through the logic in the rest of the function.</p><p>Each time a user fills a shape with a color and triggers this function, we only want to add the color added to our swatch collection if it <em>isn’t already present</em>. To achieve this, we’ll use a conditional:</p><p>if(!this.state.presetColors.includes(activeColor))</p><p>Remember when we made sure our active color was stored as a hex value in uppercase format? Since we know all our colors in our presetColors array are stored in the same format, we can check to see if the array contains the color that was just used on our canvas. If it doesn’t, then we move into the block to update state.</p><p>setState<strong> </strong>is a built-in React function that lets us update our state. Because we’ll need to build on the current values in our presetColors array, we’ll pass in a callback function to setState. (If you’re updating state based on previous state, it’s best practice to use this format because of setState’s asynchronous nature). When we pass a function to setState, this function can access our component’s state (often called <em>previous state</em>) as an argument.</p><p>We’ll pass in an argument named state<strong> </strong>to represent this previous state.<strong> </strong>Keeping with best practices, we’ll avoid mutating state directly and create a copy of our presetColors array for editing.</p><p>const colors = state.presetColors.slice(0)</p><p>Next we’ll add the used color to the <em>beginning</em> of our array and remove the <em>last</em> color so our array stays at a length of 15 colors.</p><p>colors.unshift(activeColor)<br>colors.pop()</p><p>Finally, we’ll return the updated value of presetColors, updating our local state. Because our SketchPicker relies on the value of presetColors, it will be re-rendered and the row of 15 swatches will now include the just-used color. Just what we want!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F1oYeIsOT7YA%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D1oYeIsOT7YA&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F1oYeIsOT7YA%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/04ddaf20522ebe4de83ebabfa3b5b804/href">https://medium.com/media/04ddaf20522ebe4de83ebabfa3b5b804/href</a></iframe><h3>Wrapping Up</h3><p>We now know how to customize the SketchPicker component available through <a href="https://casesandberg.github.io/react-color/">React Color</a>. By providing ways to adjust preset colors, width, hover effects, and alpha features, the component becomes a flexible and easy-to-adapt interface for color selection. Other color pickers come with their own customizations, so browse the React Color docs to learn more. If you want to customize even further, check out the section about <a href="https://casesandberg.github.io/react-color/#create">creating your own color picker</a> using their helper components.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b1eb09724ed6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Making an interactive SVG map with OO-JavaScript]]></title>
            <link>https://staceymck.medium.com/making-an-interactive-svg-map-with-oo-javascript-2c5abc73481c?source=rss-38e949a44cff------2</link>
            <guid isPermaLink="false">https://medium.com/p/2c5abc73481c</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[svg]]></category>
            <dc:creator><![CDATA[Stacey McKnight]]></dc:creator>
            <pubDate>Thu, 08 Apr 2021 21:30:09 GMT</pubDate>
            <atom:updated>2021-04-10T23:39:17.491Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="SVG map based on the St. Louis Zoo with six distinct zones" src="https://cdn-images-1.medium.com/max/1024/1*eM2JOqQXfEc6oDgGQd2RWw.png" /><figcaption>Graphic based on map data made available by <a href="https://www.openstreetmap.org/copyright">© OpenStreetMap contributors</a> &amp; <a href="https://www.stlzoo.org/">St. Louis Zoo</a> exhibits</figcaption></figure><p>The hippo exhibit in the St. Louis Zoo contains a pool of water bordered by a glass wall. Watching the hippos move underwater reveals why hippos are named after a Greek word meaning ‘river horse’. Their movements are much like the galloping of a horse as they push off the ground to propel themselves around underwater.</p><p>The <a href="https://www.stlzoo.org/">St. Louis Zoo</a> has been a favorite spot of mine since moving to the city. So when planning a project to strengthen my JavaScript (JS) skills, I decided to make a simple, interactive map based on the zoo and its six zones. Users can click on zones inside an SVG map and get back information about the zone and its exhibits pulled from a Rails API backend.</p><p>In this post, we’ll walk through the basics of setting up this interactive SVG element using CSS, JS and HTML. Specifically, we’ll explore:</p><ul><li>Creating a custom SVG in Adobe Illustrator</li><li>Working with classes in object-oriented JS</li><li>Manipulating DOM elements within a single page application</li></ul><p>In some of the examples, you’ll see classes prefixed with a “js-”. To help decouple my CSS styling and JS actions, I added this prefix to identify classes targeted by JS. You can check out the full <a href="https://github.com/staceymck/stl-zoo-map-frontend">frontend</a> and <a href="https://github.com/staceymck/stl-zoo-map-backend">backend</a> repos for more context on GitHub.</p><h3>Creating the SVG</h3><p>If your project requires a custom map or other SVG element, you can use a vector graphic editing program like Adobe Illustrator to create your image. I based my graphic on public domain map data of the zoo made available by <a href="https://www.openstreetmap.org/copyright">© OpenStreetMap contributors</a>.</p><p>When creating your own SVG in Illustrator, make layers your friend. When you export your image as an SVG, each layer gets wrapped in group tags, &lt;g&gt;&lt;/g&gt;. Each group also includes an id attribute equal to the name you set for the layer. This will help with targeting elements with JS later. For the zoo map, I set up a new layer for each zone so each map zone could be targeted and styled separately.</p><figure><img alt="SVG map inside Adobe Illustrator with six layers. Each layer contains the graphic elements composing a separate zone on the map." src="https://cdn-images-1.medium.com/max/1024/1*upKr3B0OTSjbUmALlKNxvw.png" /></figure><p>After exporting your file as an SVG, you can open it up in a code editor for fine tuning. If the SVG you’re working with doesn’t have line breaks or indentation, adding those can help you understand the structure of the graphic and what each &lt;g&gt; group contains.</p><p>Here’s what the beginning of the zoo map SVG looked like with spacing adjustments.</p><p><strong>NOTE:</strong> The paths in the code samples below have been shortened with ellipses to save space.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b3ad0ac72e829860e4c5f502490746a8/href">https://medium.com/media/b3ad0ac72e829860e4c5f502490746a8/href</a></iframe><p>In the code above, you’ll notice a few things. First, our layer name is showing up as an id inside our first group. The group contains two paths, one representing the zone shape and the second representing the animal icon it contains.</p><p>You’ll also notice that each path was generated with a class. These classes are referenced in the &lt;style&gt; tag at the top, which assigns the initial colors for the SVG paths. We can target these default classes with CSS and JS, or we can create our own more meaningfully-named classes as shown below. We can also add &lt;title&gt; tags with a short description for each group to aid accessibility.</p><p><strong>Our updated SVG:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6245c9dffa89b7edc75ad67f24397ed0/href">https://medium.com/media/6245c9dffa89b7edc75ad67f24397ed0/href</a></iframe><p>How you set up ids and classes can depend on what elements you want to style or target together. There’s no one right way, so find an approach that makes sense for your context.</p><p>One thing you might notice is that some groups contain a &lt;polygon&gt; tag instead of only &lt;path&gt; tags. Why? When we exported our SVG, the polygon tag was generated for elements composed only of straight lines, while the path tag was generated for elements that contain curves.</p><h3>Making the Map Interactive</h3><p>Now that we have our map created, let’s make it interactive with the following features:</p><ul><li>When a user hovers over a zone, add a thicker border and make the zone color darker</li><li>When a user clicks a zone to select it, let that ‘active’ zone stay colored while the other ‘inactive’ zones become gray</li><li>When a zone is selected, display that zone’s information beside the map and its exhibits underneath</li></ul><h4><strong>End goal:</strong></h4><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FiLKpLYydEIY%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DiLKpLYydEIY&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FiLKpLYydEIY%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/5f769bd1e0851a237a640f03f1763d4a/href">https://medium.com/media/5f769bd1e0851a237a640f03f1763d4a/href</a></iframe><p>There are a variety of ways to achieve these goals, but we’ll walk through an approach using object-oriented JS and classes. We’ll start by setting up three JS classes: Zone, Exhibit and MapZone.</p><p>If you haven’t encountered JS classes before, it’s helpful to know that the keyword this when used inside a class refers to the specific instance of the class we’re creating/working with.</p><h4>Class 1: Zone</h4><p>When the app first loads, our Zone class is used to create a zone object for each of our six zones using data about the zone fetched from a Rails API backend. Each zone object contains properties such as the zone’s name, unique database id, description, and its related map zone element. We can reference the value of these properties in the code with dot notation, zone_1.description, or bracket notation, zone_1[“description”].</p><p>This class is separate from the MapZone class, which focuses on the behavior and properties of the SVG map zones.</p><p><strong>Setting up the Zone class:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bbc5583db742f27ba072203959c7a762/href">https://medium.com/media/bbc5583db742f27ba072203959c7a762/href</a></iframe><p>Since we set custom class names on the map elements based on the zone names, we can use these classes to find each zone’s matching map element.</p><p>Notice that we set the database id we received from the API as a data attribute on the zone’s related map element. This will come into play when we set up our map zone objects next.</p><h4>Class 2: MapZone</h4><p>When the app starts, six instances of the MapZone class are also created. The MapZone constructor sets properties for each map zone object, such as the group element and the specific SVG path/polygon the object represents. Having an easy way to access this path element separate from the map group as a whole will help us change the zone’s fill color without changing the color of the animal icon also contained in the map group.</p><p><strong>Setting up our MapZone class:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b5df901530b1fad5e2d3da889d2a464f/href">https://medium.com/media/b5df901530b1fad5e2d3da889d2a464f/href</a></iframe><p>In our constructor, we’ll also add a property, zoneId<strong>,</strong> and set it equal to the id of its related zone object. Remember that we set a data attribute, id<strong>,</strong> on each map zone path when we created our Zone instances. This id comes from the Zone table of the database we’re accessing through the backend API. Each zone record in the database has a unique id value, which makes it a good attribute to use when we want to reference a specific zone. Since each map path has a data attribute equal to the id of the zone it represents, we can pull this data attribute id value and use it to identify the specific zone object we want information from when the map section is clicked.</p><p>Each new map zone is pushed into a static array, all, which we’ll use when setting up our handleClick method. Note that we added event listeners for click, mouse-in and mouse-out events on each map group. These will come into play coming up.</p><h4>Class 3: Exhibit</h4><p>Let’s take a look at the Exhibit class next. Since there are a limited number of exhibits, I decided to send a fetch request to the backend API when the app starts. However, you could set this up to only send a fetch request when a map zone is clicked instead of preloading all the related content for your map.</p><p><strong>Setting up our Exhibit class:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c8ddaa96c8f11ecfc04cefcc0142a9c/href">https://medium.com/media/5c8ddaa96c8f11ecfc04cefcc0142a9c/href</a></iframe><p>Each exhibit will be rendered as a card, so we’ll add an element property to every exhibit object to represent that card div. We’ll add our related CSS card class to this div. Next we attach an event listener to each card that will allow the card to toggle between displaying the main exhibit info (front) and a fact about the exhibit’s species (back) with each click.</p><p>We’ll also make sure to push each exhibit into our empty Exhibit.all array so when a map zone is clicked, we can filter the array and only select exhibits that have a matching zone id property.</p><h4>Handling Clicks and Hovering</h4><p>Now that our central classes are set, we need to listen for events on the map, specifically clicks and hover states, so we can apply different styles based on user interaction. While CSS allows us to change styling based on hover state, I decided to use JS to handle both clicks and hovering effects since they both relate to map interaction.</p><p>In our MapZone constructor, we set three event listeners on each individual map group. This allows us to trigger a specific action, or callback function, when a specific event type occurs.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/29acf09b13f9320e2b1c4458096720eb/href">https://medium.com/media/29acf09b13f9320e2b1c4458096720eb/href</a></iframe><p>We’ll start by handling hover states, so let’s set up handleMouseEnter and handleMouseLeave functions in our MapZone class as instance methods. Thinking back to our goal, we want to darken the color of a zone when a user moves the mouse over it (mouseenter event) and return it to its original color when the user moves the mouse out (mouseleave event).</p><p>We can achieve this by adding a class, js-hover, to the map path being hovered over that we can style in the desired way with CSS.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/df21bcaef37a4b92172417bda6c02ed7/href">https://medium.com/media/df21bcaef37a4b92172417bda6c02ed7/href</a></iframe><p>Our next goal is to change zone colors based on click events, setting inactive/non-selected zones to gray, with a darker gray color on hover, and active/selected zones to their original color without any other effects. To do this, we can add an additional class that indicates whether a zone is ‘active’ or ‘inactive’ based on clicks. Once we set up these additional classes, we can combine the class selectors in our CSS to create styles for all possible interaction states (.js-hover.js-active — user hovers over active zone, .js-hover.js-inactive — user hovers over inactive zone, etc.).</p><p>We’ll set up our click-triggered features in a handleClick method inside our MapZone class so that all instances of the class (i.e., each map zone) will have access to it.</p><p><strong>Setting up our handleClick method:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3d51d0067cadd5e8099134c09b499765/href">https://medium.com/media/3d51d0067cadd5e8099134c09b499765/href</a></iframe><p>Let’s walk through this handleClick method, which gets triggered each time a map zone in the SVG is clicked.</p><p>First we toggle whether a map zone has an active or inactive class based on which map zone has been clicked. These classes will apply our chosen styling effects that change the color and hover styles of the map zones.</p><p>Next we find the zone object from our Zone class that contains the information about the selected zone we want to display beside the map. We’ll call a method, attachToDom()<strong>,</strong> on this zone object. This method will build out the zone element we want to add to the DOM and display it beside the map.</p><p>Our exhibits container and exhibits card container are present on the DOM from the start of the app, but they aren’t yet visible on the page. Their display style is initially set to ‘none’, so to make them visible we need to change that to ‘block’. We also need to make sure we have a fresh slate inside our exhibit card container with each new click so we don’t display any exhibits that might have been added to the DOM with a previous click.</p><p>Now we’re ready to filter our Exhibit.all array that holds all of our zoo exhibits and find the exhibits that match the selected zone and attach each one to the DOM.</p><p>Since we’re adding to the length of our page when we add the exhibit cards, we’ll also make a button visible that allows users to scroll back up to the top with one click.</p><h3>Wrapping Up</h3><p>Targeting SVG elements with JS can be a fun way to add interaction to web applications. We’ve seen how layers can help keep custom SVGs organized and we can add JS event listeners to different paths within an SVG. JS opens up endless options for adding engaging elements to your apps, so let the inspiration run wild. 🦛</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2c5abc73481c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[An Intro to Partials and Helpers in Ruby on Rails]]></title>
            <link>https://staceymck.medium.com/an-intro-to-partials-and-helpers-in-ruby-on-rails-10d62d85da24?source=rss-38e949a44cff------2</link>
            <guid isPermaLink="false">https://medium.com/p/10d62d85da24</guid>
            <category><![CDATA[helpers]]></category>
            <category><![CDATA[partial]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[rails]]></category>
            <dc:creator><![CDATA[Stacey McKnight]]></dc:creator>
            <pubDate>Fri, 12 Mar 2021 16:48:35 GMT</pubDate>
            <atom:updated>2021-03-12T16:48:35.260Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jvRDuJGI5lovtARKQuGkyA.png" /><figcaption>Source: <a href="https://undraw.co/">undraw.co</a></figcaption></figure><p>After college I coordinated volunteer groups at a shelter for families experiencing homelessness. The largest service day involved coordinating assignments for 70 volunteers. The shelter had mostly smaller rooms, so I split the group up, with each subgroup working in a separate space on a separate task. This division of work allowed us to accomplish a range of discrete projects while not jamming the halls with lots of movement between rooms or requiring the same project instructions to be repeated from one room to the next.</p><p>This highly-organized day came to mind recently as I was building a Ruby on Rails app that helps users track volunteer engagements at their organization. Rails favors conventions that lend organization to applications and uses a Model-View-Controller (MVC) architecture that separates application responsibilities into three distinct layers:</p><ul><li><strong>Models</strong> handle the data and domain logic</li><li><strong>Views</strong> handle presentation and how information is displayed to users</li><li><strong>Controllers</strong> serve as the intermediary between views and models as they relay user requests to the model for processing and pass on model responses to the views for display</li></ul><p>As part of this separation of concerns, views and models don’t directly interact. Models don’t handle how information is displayed, and it’s generally best practice to keep logic out of your view files as much as possible.</p><p>In this post we’ll explore how Rails partials and helpers can help us follow the ‘Separation of Concerns’ principle of software design while also avoiding repetition and keeping our views <a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY.</a> We’ll use snippets from the volunteer application as examples.</p><h3>Helpers</h3><h4>What are helpers?</h4><p>Helpers are methods that allow you to encapsulate tasks for reuse throughout your views. They can also help you extract logic from your views, like conditional statements. Rails provides handy pre-built view helpers that assist with common tasks like building forms and creating HTML links. If the built-in helpers don’t fit your needs, you can also build your own custom helper methods.</p><h4>Where to store custom helpers</h4><p>Custom helpers are typically stored in the app/helpers directory and organized into modules named after your controllers. In the volunteer application, for example, I created a VolunteersController and a volunteers_helper.rb<strong> </strong>file that contains a VolunteersHelper<strong> </strong>module. This module stores my custom helper methods for displaying volunteer-related data.</p><p>While you can use the helper methods defined in the app/helpers directory in any view, separating the helper methods into different modules and files based on how they’re used can make development and debugging easier.</p><p>There might be a helper method you want to use in multiple views. In this case, Rails provides an ApplicationHelper<strong> </strong>module (found in<strong> </strong>application_helper.rb)<strong> </strong>where you can store helpers that span multiple categories.</p><h4>Examples</h4><p><strong>1: link_to - </strong>In the volunteer tracking application, I made frequent use of the built-in Rails helper method <a href="https://apidock.com/rails/v6.0.0/ActionView/Helpers/UrlHelper/link_to">link_to</a>. This method is handy for setting up links in your views and lets you make use of the path/url helpers Rails provides when you set up routes.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/47638c2d24d853534f874e7560258b29/href">https://medium.com/media/47638c2d24d853534f874e7560258b29/href</a></iframe><p>This snippet generates the following HTML:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/60370bf33d1e8b49f319787f1b60acdf/href">https://medium.com/media/60370bf33d1e8b49f319787f1b60acdf/href</a></iframe><p><strong>2: image_tag - </strong>Along with a helper for generating links, Rails provides a helper for displaying images. The <a href="https://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html#method-i-image_tag">image_tag</a> helper generates a HTML image tag with the resource specified. This tag came in handy for inserting an image placeholder into volunteer profile pages.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/59469d87abb659b5813969317bb53e07/href">https://medium.com/media/59469d87abb659b5813969317bb53e07/href</a></iframe><p>This snippet generates the following HTML in the browser:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/109246a21b64e2959267e445bc9807df/href">https://medium.com/media/109246a21b64e2959267e445bc9807df/href</a></iframe><p>Rails also has extensive <a href="https://guides.rubyonrails.org/v6.0/form_helpers.html">helpers for form building</a> along with helpers for <a href="https://guides.rubyonrails.org/action_view_helpers.html#number-to-currency">formatting currency</a>, <a href="https://guides.rubyonrails.org/action_view_helpers.html#number-to-phone">formatting phone numbers</a>, and more <a href="https://guides.rubyonrails.org/action_view_helpers.html#urlhelper">url/link related helpers</a>.</p><p><strong>Side note:</strong> If you’re looking for beautiful open-source illustrations, check out <a href="https://undraw.co/">Undraw.co</a>.</p><h3>Partials</h3><h4>What are partials?</h4><p><a href="https://guides.rubyonrails.org/layouts_and_rendering.html#using-partials">Partial templates</a> help you package chunks of code for reuse in your views. Partials are a good fit for code you find yourself repeating, like a snippet you want to display for each item in a collection.</p><p>In the volunteer app, for example, I used the same form structure to create a new volunteer record and edit an existing volunteer record. Rather than creating the form twice, I created the form once as a partial and rendered it in both volunteers/new.html.erb and volunteers/edit.html.erb.</p><h4>Where to store partials</h4><p>Partial file names start with an underscore, like _volunteer.html.erb. They’re typically organized into view folders based on which views use them. Continuing the volunteer form example, I stored the form partial in my app/views/volunteers directory since I would be using it in only volunteer-related views.</p><p>You can set up a shared folder in your app/views directory to hold partials you use across your application, such as error messages.</p><h4>Partials in use</h4><p><strong>Rendering a partial: <br></strong>The <strong>render</strong> method lets you incorporate your partials into your views. If you’re rendering the partial in a view that’s located in the <em>same view folder</em> as the partial, you can simply reference the partial by name (without the leading underscore):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fcc1698713835d0ccf9ad686848f6993/href">https://medium.com/media/fcc1698713835d0ccf9ad686848f6993/href</a></iframe><p>If you’re rendering a partial from another folder, be sure to include the folder name too (still no leading underscores needed):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e99248cec74ff537826b588139b27db1/href">https://medium.com/media/e99248cec74ff537826b588139b27db1/href</a></iframe><p><strong>Partials with locals</strong></p><p>For greater flexibility, partials can be set up with local variables. In the volunteer app, if a user submits a form to create a volunteer, role, or activity record that contains invalid data, the app displays <a href="https://guides.rubyonrails.org/active_record_validations.html#displaying-validation-errors-in-views">Rails’ standard validation error messages</a> above the related form so users will know how to adjust the input before resubmitting.</p><p>Since I wanted the code to work for any object (e.g., volunteer, role, activity record), I set up a _validation_errors.html.erb<strong> </strong>partial in the<strong> </strong>views/shared<strong> </strong>directory<strong> </strong>using a local variable named object<strong>.</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/80a36d7fb555c61c4981ebb43898412f/href">https://medium.com/media/80a36d7fb555c61c4981ebb43898412f/href</a></iframe><p>To render this partial in my views/volunteers/new.html.erb template, we must pass in a value for object. What value though?</p><p>In my VolunteersController’s<strong> </strong>new action, I instantiated a new volunteer object, @volunteer<strong>, </strong>for the user to add attributes to via the ‘new volunteer’ form. If the user fills out the form in a way that meets all the validations/criteria set up in my Volunteers model (e.g., a volunteer must have a full name, must have a contact method), then the new volunteer record will be saved to the database. If not, Rails will add error messages to my @volunteer<strong> </strong>object, so I’ll need to pass<strong> </strong>@volunteer to the partial to access these messages.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b747d6bed817eedf22dd62999a482834/href">https://medium.com/media/b747d6bed817eedf22dd62999a482834/href</a></iframe><p>Over in the template for making a new role things work the same way:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/166a2ff845731a6ef31582695e7b3b45/href">https://medium.com/media/166a2ff845731a6ef31582695e7b3b45/href</a></iframe><p>And in the template for creating a new activity:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/01cc1390baf726bedb2d37e3d49966e8/href">https://medium.com/media/01cc1390baf726bedb2d37e3d49966e8/href</a></iframe><h4>Rendering Collections with Partials</h4><p>One way to extract iteration logic from your views is to use a partial and specify a collection option. Rather than including the following loop <strong>directly</strong> in the volunteers index template, this can be refactored with a partial.</p><p><strong>Loop in the index view:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d827c26f65b99cab31613e9bfef76a63/href">https://medium.com/media/d827c26f65b99cab31613e9bfef76a63/href</a></iframe><p><strong>Refactored with partial:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/84f6febec8dad4a966e4f784f74aaa7d/href">https://medium.com/media/84f6febec8dad4a966e4f784f74aaa7d/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/92b01a2073782e427db5d841084c78dc/href">https://medium.com/media/92b01a2073782e427db5d841084c78dc/href</a></iframe><h3>Bonus: Spacer Templates</h3><p>If you want to add a specific element between each item of a collection, like a horizontal rule or a comma, you can use Rails’ <a href="https://guides.rubyonrails.org/layouts_and_rendering.html#spacer-templates">spacer template</a> option when rendering your collection.</p><h4><strong>Example:</strong></h4><p>In the volunteer app, each volunteer’s profile page displays a list of all the volunteer roles they’ve engaged in. Each role is displayed as a link to that role’s show page, and the list is separated by commas.</p><p>First we set up a partial with a local variable,<strong> </strong>role, for the role links:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d6324c112b9167160a6b7b25a7a9b194/href">https://medium.com/media/d6324c112b9167160a6b7b25a7a9b194/href</a></iframe><p>Then we create possibly the simplest file ever to serve as the spacer template:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef690f35bbaa238e923e2025d0c01202/href">https://medium.com/media/ef690f35bbaa238e923e2025d0c01202/href</a></iframe><p>Now the partial and spacer template can be combined to display the list in the profile page:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/47ef523ac769c03ee254cf0ba34c96f6/href">https://medium.com/media/47ef523ac769c03ee254cf0ba34c96f6/href</a></iframe><h3>Keep learning</h3><p>Partials and helpers are handy tools for DRYing out your Rails app and separating concerns. We’ve explored some of the basic applications and syntax here. If you want to keep learning, the Rails docs provide more examples and details on the methods and options available to you. Check them out!</p><ul><li><a href="https://guides.rubyonrails.org/layouts_and_rendering.html">Layouts and rendering </a>(including partials)</li><li><a href="https://guides.rubyonrails.org/action_view_helpers.html">Action View helpers</a> (overview)</li><li><a href="https://guides.rubyonrails.org/form_helpers.html">Form helpers</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=10d62d85da24" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Working with params in a Sinatra MVC app]]></title>
            <link>https://staceymck.medium.com/working-with-params-in-a-sinatra-mvc-app-a0d9ae9faf3a?source=rss-38e949a44cff------2</link>
            <guid isPermaLink="false">https://medium.com/p/a0d9ae9faf3a</guid>
            <category><![CDATA[mvc]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[sinatra]]></category>
            <category><![CDATA[nested-params]]></category>
            <category><![CDATA[params]]></category>
            <dc:creator><![CDATA[Stacey McKnight]]></dc:creator>
            <pubDate>Wed, 10 Feb 2021 17:34:03 GMT</pubDate>
            <atom:updated>2021-02-19T17:42:05.684Z</atom:updated>
            <content:encoded><![CDATA[<p>Want to build a simple web application in Ruby? Consider <a href="http://sinatrarb.com/intro.html">Sinatra</a>.</p><p>Sinatra is a domain specific language, or DSL, that serves as a lightweight Ruby framework for creating simple web apps. Chances are, you’re already working with Sinatra if you’re reading this post.</p><p>While Sinatra doesn’t require you to follow the common Model-View-Controller <a href="https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller">(MVC)</a> design pattern for your application, building an MVC app with Sinatra can help you learn how these pieces — models, views, and controllers — work together.</p><p>I recently worked with Sinatra to build a database-backed MVC application that uses <a href="https://guides.rubyonrails.org/active_record_basics.html">Active Record</a>. The app helps job seekers track the status of their job applications and add related follow-up reminders for themselves.</p><p>Through this post we’ll explore how params work within a Sinatra-based MVC app, and we’ll draw on the job app tracker for examples. Specifically, we’ll explore how params allow us to:</p><ul><li>Interact with user-supplied data</li><li>Set up dynamic routes</li><li>Work with complex forms</li></ul><p>The examples are simplified to help us focus in on params-related concepts. If you want to add more context and see how other features like validations, checkboxes, and additional model attributes come into play, check out the full repo for the job app tracker project <a href="https://github.com/staceymck/job-app-track">here.</a></p><h3>Routes</h3><p>To understand Sinatra params, we first need to understand routes. Sinatra routes allow you to field HTTP requests from a user’s browser and send back a response based on the URL a user visited and the type of <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods">HTTP request</a> they made (GET, POST, PATCH, etc.).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0d12a400ce1c351cd2d6dcee5e27c68e/href">https://medium.com/media/0d12a400ce1c351cd2d6dcee5e27c68e/href</a></iframe><p>Say we created this route for our site, job-app-track.com, inside a controller class we named JobAppsController. When a user visits https://job-app-track.com<strong>/job_apps</strong>, a GET request is sent to our server. Our JobAppsController matches the request to our get ‘/job_apps’ route, and the response ‘Hello, World’ is sent to the user’s browser for display.</p><p>Now on to params.</p><h3>What are params?</h3><p>In Sinatra the term params, short for parameters, refers to a hash containing key-value pairs that is accessible inside route blocks in your controllers. (If you dig into <a href="https://rubydoc.info/gems/sinatra/Sinatra%2FBase:params">Sinatra docs</a>, you’ll find that ‘params’ is a method that returns the value of attribute params. However, the term ‘params’ is generally used to refer to the hash return value rather than the method itself.)</p><p><strong>Dynamic contents<br></strong>The params hash is encoded in an HTTP request and includes data that helps us process that request within a controller. Because of this dynamic nature, the key-value pairs our params hash contains generally differ from one route to the next.</p><p>For example, if we build a dynamic route like the one below, our params hash will contain a key-value pair based on the URL a user visits (routing params).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/da666a355b04725cbef87df98e7879d4/href">https://medium.com/media/da666a355b04725cbef87df98e7879d4/href</a></iframe><p>If a user navigates to https://job-app-track.com/job_apps/<strong>applied</strong>, our params will contain the following key-value pair: {“status” =&gt; “applied”}</p><p>We can access the value of this pair, “applied”, inside our route. In Sinatra, params is a <a href="https://www.rubydoc.info/gems/sinatra/Sinatra/IndifferentHash">Sinatra::IndifferentHash</a> object, which means you can refer to keys as symbols params[:status] or as strings params[“status”] .</p><h3>User-supplied params</h3><p>Along with routing-related parameters, our params hash can also contain user-supplied parameters. These come into play when working with forms.</p><p>Below we have a simple HTML form that lets users track a new job application. The values of the name attributes in our input elements will serve as keys in the params hash. The method and action we specify for our form determines which route has access to this particular params hash. In this case we can access it inside our post ‘/job_apps’ route block.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8b165add1e59027ceb33e729463778e4/href">https://medium.com/media/8b165add1e59027ceb33e729463778e4/href</a></iframe><p>When a user fills out the form, the params hash encoded in the HTTP post request will be structured like this:</p><p>{“job_title” =&gt; “Editor”, “company” =&gt; “Magazine XYZ”}</p><p>Inside our corresponding route block, we can access the user-supplied values. In the example below, we use the params hash to create a new instance of a job application, save it to our database, and redirect the user to the route that will display that job application’s details.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/80dd4cde720afe778f3b261896fab973/href">https://medium.com/media/80dd4cde720afe778f3b261896fab973/href</a></iframe><p><strong>A deeper dive:</strong> <br>So what’s happening on line 4? When using Active Record, a successful save means the instance we saved is given a unique <strong>id</strong>, or <a href="https://guides.rubyonrails.org/active_record_migrations.html">primary key</a>, in the database. We can access the value of this id by calling id on our saved application instance: app.id. This id can be used to set up a dynamic route similar to the dynamic status route we defined above.</p><p>The code below shows how we can set up this dynamic route. First we use our model class, JobApp, to look in our database and find the specific job app the user requested based on the number included in the URL. If the URL a user enters is “https://job-app-track.com/job_apps/15&quot;, then the program will look for a job app with an id of 15.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/58af57e3ccb65d86558822ac929064a5/href">https://medium.com/media/58af57e3ccb65d86558822ac929064a5/href</a></iframe><p>Line 3 renders an <a href="https://apidock.com/ruby/ERB">erb (embedded Ruby) template</a>, show.erb, located inside a job_apps folder. This file falls under the category of a “view” in our MVC pattern.</p><p>When we <strong>render</strong> a view in our route block (instead of redirecting), that view file has access to any instance variables we created inside the block. In this case, we can access the selected @app in our show.erb file and and display attributes like @app.job_title and @app.company to the user.</p><h3>Nested params</h3><p>Nested params prove useful when you need to build a complex form that collects user-supplied data about more than one model at a time.</p><p>Suppose we want our users to be able to add a follow-up action when they create a new job application record. A feature like this could come in handy if a user wants to track a job they’re interested in and set a reminder to apply by a certain date.</p><p>Using Active Record, we can set up the desired relationships between our two models, job apps and follow-ups. A job app ‘has many’ follow-up actions, and a specific follow-up action ‘belongs to’ a particular job app. We then move on to our JobAppsController class and job app-related views. Rather than having users complete one form to create a job app and then a separate form to add an initial follow-up action, we can set up a single form that lets users do both at the same time if they choose. We’ll start by setting up a route in our JobAppsController that will render our ‘new job app’ form for the user.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/90e95b1cdae5e93d529a8d8a8aef97c8/href">https://medium.com/media/90e95b1cdae5e93d529a8d8a8aef97c8/href</a></iframe><p>If you’re curious why the route is written this way, it follows <a href="https://www.learnhowtoprogram.com/ruby-and-rails/routing-with-ruby/basics-of-routing">RESTful</a> naming conventions. <a href="http://www.restular.com/">Restular</a> is a helpful tool for building your own RESTful routes in Sinatra.</p><p>With our route set up, we’ll now build a form in a view file, new.erb, inside a folder, job_apps, that holds all our job application-related views. (If your program has multiple models, creating a main views directory with subfolders for each model’s views can help keep things organized.)</p><p>Within our new.erb file, we’ll set up a few input fields for our job app’s attributes and a section for adding a follow-up action.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/963965a23d3ed012287435d1c6036c1f/href">https://medium.com/media/963965a23d3ed012287435d1c6036c1f/href</a></iframe><p>Notice that the values of our name attributes in the input elements aren’t set equal to single words like they were in the first form we built (e.g., name=”title”). Instead, we’ve used a format that sets up a <strong>nested </strong>params hash for us that contains job_app and follow_up as the top-level keys.</p><p>A sample params hash will now look something like this when a user submits the form:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d47f6182aad6eecab5250a82d2b53f4/href">https://medium.com/media/8d47f6182aad6eecab5250a82d2b53f4/href</a></iframe><p>Over in our JobAppsController, we can set up the following POST route to interact with this hash. It’s possible a user will only want to create a job app and not add an initial follow-up action at the same time, so we can take that into account with a conditional statement that checks if the form was submitted with empty follow-up values.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fcca7998606c71a9494486eab1c94671/href">https://medium.com/media/fcca7998606c71a9494486eab1c94671/href</a></iframe><p><strong>A deeper dive:</strong> Active Record’s <a href="https://guides.rubyonrails.org/active_record_basics.html#create">create method</a> creates a new object <strong>and </strong>saves it to the database. In line 4, create takes on a new form as a <a href="https://api.rubyonrails.org/v6.1.2/classes/ActiveRecord/Associations/CollectionProxy.html#method-i-create">collection proxy method</a> because we call it on app.follow_ups, which is considered a collection even though the job application doesn’t have any follow-ups yet. This method is made possible because of the has_many <a href="https://guides.rubyonrails.org/association_basics.html#has-many-association-reference">relationship</a> we established between our job app and follow-up models. It’s important to note that it only works if the base model for the collection (in this case app) is already saved to the database.</p><p>Now that we’ve set up our form to make our first job app and follow-up action, we can check our work.</p><p>We’ll require a <a href="https://rubygems.org/gems/pry/versions/0.13.1">gem called Pry</a> in our JobAppsController file and add the method call binding.pry inside our post ‘job_apps/’ route above the redirect line. When our controller fields a request for that route (i.e., when a user submits the related form), the program will pause and allow us to enter a pry console where we can examine the values of the instances we created using the user-submitted data. The sample console session below utilizes some <a href="https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html">special methods</a> provided to us through the relationships we set up between our models.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a587bbd4c43584be4c59975e28fa13f5/href">https://medium.com/media/a587bbd4c43584be4c59975e28fa13f5/href</a></iframe><h3>Keep learning</h3><p>The Sinatra params hash is a flexible and powerful tool, especially when using forms. They can help you set up dynamic routes, interact with user-supplied data in your controllers, and build complex forms. Nested hashes can have multiple top level keys and multiple layers depending on your needs.</p><p>One good way to learn more about params is to set up your own simple Sinatra app and experiment. Require the <a href="https://rubygems.org/gems/pry/versions/0.13.1">gem Pry</a> and add the line binding.pry inside a POST, PATCH or PUT route that receives a form submission. Submit the related form through your browser, and a pry session should start in your console. Type inparams to see what your params hash looks like. You can also test this out with a dynamic GET route.</p><p>Thanks for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a0d9ae9faf3a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>