<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rhys Malyon</title>
    <description>The latest articles on DEV Community by Rhys Malyon (@rhysmalyon).</description>
    <link>https://dev.to/rhysmalyon</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F668133%2F012abc61-c2ba-4e75-b019-62d569358b7c.jpeg</url>
      <title>DEV Community: Rhys Malyon</title>
      <link>https://dev.to/rhysmalyon</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rhysmalyon"/>
    <language>en</language>
    <item>
      <title>Making pageContext available to child components in Gatsby with react-intl</title>
      <dc:creator>Rhys Malyon</dc:creator>
      <pubDate>Tue, 16 Nov 2021 04:47:27 +0000</pubDate>
      <link>https://dev.to/rhysmalyon/making-pagecontext-available-to-child-components-in-gatsby-with-react-intl-49ch</link>
      <guid>https://dev.to/rhysmalyon/making-pagecontext-available-to-child-components-in-gatsby-with-react-intl-49ch</guid>
      <description>&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Background&lt;/li&gt;
&lt;li&gt;The Problem&lt;/li&gt;
&lt;li&gt;The Solution&lt;/li&gt;
&lt;/ol&gt;



&lt;h2&gt;
  
  
  1. Background
&lt;/h2&gt;




&lt;p&gt;Check out my &lt;a href="https://www.rhysmalyon.com" rel="noopener noreferrer"&gt;portfolio&lt;/a&gt;, &lt;a href="https://github.com/RhysMalyon" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;, or add me on &lt;a href="https://www.linkedin.com/in/rhysmalyon" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; to get in touch. I'm always looking for new connections or feedback!&lt;/p&gt;




&lt;p&gt;I'm currently based out of Japan which means that, when it comes to web development projects, I sometimes have to deal with bilingual websites. When building a static website with HTML, CSS, and JavaScript, it's as easy as creating a new HTML file for each page, copy pasting a basic structure, and pasting in the Japanese copy. &lt;/p&gt;

&lt;p&gt;But what happens when you want your website to be more dynamic? What if you want to take advantage of the performance benefits that some frameworks offer? This is a challenge I faced recently when migrating a static website I'd made as a freelance project to Gatsby.&lt;/p&gt;

&lt;p&gt;Sure, an argument could be made that going the framework route for static websites might be overkill, but there are &lt;a href="https://medium.com/geekculture/why-should-you-learn-gatsby-in-2021-what-are-the-benefits-448231c61fe4" rel="noopener noreferrer"&gt;a lot of benefits&lt;/a&gt; that platforms such as Gatsby have to offer.&lt;/p&gt;

&lt;p&gt;If you're into social impact and making a difference, head over to the &lt;a href="https://socious.io" rel="noopener noreferrer"&gt;static website&lt;/a&gt; to learn more about Socious. Also Feel free to check out the (work-in-progress) &lt;a href="https://sociousgatsbymaster.gatsbyjs.io/" rel="noopener noreferrer"&gt;Gatsby website&lt;/a&gt; or have a look at the &lt;a href="https://github.com/RhysMalyon/socious-gatsby" rel="noopener noreferrer"&gt;GitHub repo&lt;/a&gt; to see how everything fits together.&lt;/p&gt;



&lt;h2&gt;
  
  
  2. The Problem
&lt;/h2&gt;




&lt;p&gt;If you, like me, are looking for a way to create dynamic, multilingual websites using Gatsby then I'd fully recommend &lt;a href="https://simplelocalize.io/blog/posts/gatsby-i18n/" rel="noopener noreferrer"&gt;react-intl&lt;/a&gt;. Installation is easy, the example code is clear and their explanations are great. There was one problem I faced as a beginner:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4vuq8akur4p5r1d65juw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4vuq8akur4p5r1d65juw.png" alt="Props error"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The problem is that part of the functionality &lt;code&gt;react-intl&lt;/code&gt;, particularly the &lt;code&gt;SimpleLocalize&lt;/code&gt; component, relies on the page's context to derive the current language. This helps determine which text to use and which links to follow but if your page is broken down into separate components like mine then you're likely going to run into this error.&lt;/p&gt;



&lt;h2&gt;
  
  
  3. The Solution
&lt;/h2&gt;




&lt;p&gt;It might seem fairly obvious but as a relative beginner to Gatsby and React it took me longer than I'd care to admit. After an embarrassing amount of Googling and digging through Stack Overflow I had a moment of clarity: what if the my page's child components didn't know the page context? &lt;/p&gt;

&lt;p&gt;I'd just assumed that, because they were being rendered on the page, that they would automatically have access to the information that &lt;code&gt;SimpleLocalize&lt;/code&gt; needed, which wasn't the case.&lt;/p&gt;

&lt;p&gt;Sure enough, in the end it was as easy as passing down the props of the main page component to the child components and everything came together. I elected to pass all of the page's props using the spread operator (&lt;code&gt;{...props}&lt;/code&gt;) but if you're committed to fine-tuning for performance feel free to destructure what you need.&lt;/p&gt;

&lt;p&gt;In the end, this is the body of my parent (homepage) component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const IndexPage = (props) =&amp;gt; {
  return (
    &amp;lt;Layout&amp;gt;
      ...
      &amp;lt;AppLinks {...props} /&amp;gt;
      ...
    &amp;lt;/Layout&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And here's how I implemented it in the child component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const AppLinks = (props) =&amp;gt; {
  return (
    &amp;lt;SimpleLocalize {...props}&amp;gt;
      ...
      &amp;lt;FormattedMessage
        id="app-links-header"
      /&amp;gt;
      ...
      &amp;lt;FormattedMessage
        id="app-links-subheader"
      /&amp;gt;
      ...
    &amp;lt;/SimpleLocalize&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;h3&gt;
  
  
  Sidenote
&lt;/h3&gt;




&lt;p&gt;For each level of nested components you'll need to pass the props along. For example, I had three layers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- index.js
-- Intro.js
--- EarlyAccessForm.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Follow the same process at each step and you'll have a working chain of components.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;Intro.js&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;EarlyAccessForm {...props.pageContext} /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And in &lt;code&gt;EarlyAccessForm.js&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
render() {
  return (
    &amp;lt;SimpleLocalize {...this.props}&amp;gt;
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I used &lt;code&gt;{...this.props}&lt;/code&gt; since &lt;code&gt;EarlyAccessForm&lt;/code&gt; is a class-based component but to use it in a functional component just change &lt;code&gt;{...this.props)&lt;/code&gt; to &lt;code&gt;{...props}&lt;/code&gt;.&lt;/p&gt;




&lt;p&gt;And voila, dynamic copy rendered based on the page's current language setting!&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn5wx4e5ss6lijjvwb417.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn5wx4e5ss6lijjvwb417.png" alt="English app links"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbdp3w4x553hqurzfsgm9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbdp3w4x553hqurzfsgm9.png" alt="Japanese app links"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;And there you have it. If you've worked with &lt;code&gt;react-intl&lt;/code&gt; before and have any suggestions, feel free to drop them down in the comments!&lt;/p&gt;

</description>
      <category>gatsby</category>
      <category>react</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Creating a Google Map component in Rails-React</title>
      <dc:creator>Rhys Malyon</dc:creator>
      <pubDate>Fri, 12 Nov 2021 14:38:20 +0000</pubDate>
      <link>https://dev.to/rhysmalyon/creating-our-google-map-component-4h88</link>
      <guid>https://dev.to/rhysmalyon/creating-our-google-map-component-4h88</guid>
      <description>&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;




&lt;ol&gt;
&lt;li&gt;React Google Maps API&lt;/li&gt;
&lt;li&gt;Creating a Map component&lt;/li&gt;
&lt;li&gt;Building our controller logic&lt;/li&gt;
&lt;li&gt;Making markers for our places&lt;/li&gt;
&lt;li&gt;Adding infowindows to markers&lt;/li&gt;
&lt;li&gt;Next steps&lt;/li&gt;
&lt;li&gt;Get in touch&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  1. React Google Maps API
&lt;/h2&gt;




&lt;p&gt;Let's jump straight into building out our map. To start with we'll need to install &lt;a href="https://react-google-maps-api-docs.netlify.app/" rel="noopener noreferrer"&gt;React Google Maps API&lt;/a&gt;, a React package that gives us a handy wrapper around the Google Maps API that gives us a range of pre-built components that we can customise to our needs.&lt;/p&gt;

&lt;p&gt;I'll be using npm for my installation but feel free to use yarn if that's what you're more comfortable with. We'll be following the instructions from the &lt;a href="https://react-google-maps-api-docs.netlify.app/#section-getting-started" rel="noopener noreferrer"&gt;package documentation&lt;/a&gt; so head to your terminal and enter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save @react-google-maps/api

# or

yarn add @react-google-maps/api
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  2. Creating a Map component
&lt;/h2&gt;




&lt;p&gt;Once the install is complete we're going to create our Map component. In your terminal type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails g react:component Map
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will work exactly the same as the HelloWorld component we created previously, creating a new file for us in the &lt;code&gt;rails-react-google-maps/app/javascript/components/&lt;/code&gt; folder. We'll be using the functional component provided in the docs so in your new &lt;code&gt;Map.js&lt;/code&gt; component file, delete all the contents and copy in the following setup:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react'
import { GoogleMap } from '@react-google-maps/api';

const containerStyle = {
  width: '100vw',
  height: '50vh'
};

const center = {
  lat: -3.745,
  lng: -38.523
};

const Map = () =&amp;gt; {
  return (
    &amp;lt;GoogleMap
      mapContainerStyle={containerStyle}
      center={center}
      zoom={10}
    &amp;gt;
      { /* Child components, such as markers, info windows, etc. */ }
      &amp;lt;&amp;gt;&amp;lt;/&amp;gt;
    &amp;lt;/GoogleMap&amp;gt;
  )
}

export default React.memo(Map)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can technically give the component any name you want but for simplicity's sake we'll stick with Map.&lt;/p&gt;

&lt;p&gt;Next let's get our map onto our page! Go back to your &lt;code&gt;index.html.erb&lt;/code&gt; file and replace the existing HelloWorld components with the following line (P.S. you can also delete the &lt;code&gt;HelloWorld.js&lt;/code&gt; file in your components folder at this point):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;%= react_component("Map") %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Restart your rails server (&lt;code&gt;Ctrl+C&lt;/code&gt; -&amp;gt; &lt;code&gt;rails s&lt;/code&gt;) and refresh your &lt;code&gt;localhost:3000&lt;/code&gt; page in your browser. Wait, where's our map?! You should probably see something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhvhao286z7j2v50aurl6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhvhao286z7j2v50aurl6.png" alt="Map error"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Don't worry, this is a good sign since it means our map component is working! Let's check our browser's developer tools console to see what's happening:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2mnl8kxw4l6v4do85q2a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2mnl8kxw4l6v4do85q2a.png" alt="Console errors"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We're still missing something: our API keys. We need the keys we generated earlier and put in our &lt;code&gt;.env&lt;/code&gt; file in order to gain access to Google Maps.&lt;/p&gt;

&lt;p&gt;Back in our &lt;code&gt;application.html.erb&lt;/code&gt; view file, paste this line of code inside the body tag. Everything should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;body&amp;gt;
  &amp;lt;%= yield %&amp;gt;
  &amp;lt;%= javascript_include_tag "https://maps.googleapis.com/maps/api/js?libraries=places&amp;amp;key=#{ENV['GMAPS_BROWSER_KEY']}" %&amp;gt;
&amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This script will load Google Maps using our browser API key. We're not quite there yet, we still have one more unused key to place somewhere! This time we'll be revisiting our geocoder gem. Go back to &lt;code&gt;rails-react-google-maps/config/initializers/geocoder.rb&lt;/code&gt; and uncomment the following lines:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;lookup: :nominatim,         # name of geocoding service (symbol)
use_https: false,           # use HTTPS for lookup requests? (if supported)
api_key: nil,               # API key for geocoding service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, change the value of each of these to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;lookup: :google,
use_https: true,
api_key: ENV['GMAPS_SERVER_KEY'],
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Refresh your &lt;code&gt;localhost:3000&lt;/code&gt; page and you should have a map showing up. It's centered on Brazil for now due to the default coordinates in &lt;code&gt;Map.js&lt;/code&gt; but we'll fix that next in the &lt;code&gt;places_controller.rb&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxenwmaql3ytfywh65btq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxenwmaql3ytfywh65btq.png" alt="Google Map"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Building our controller logic
&lt;/h2&gt;




&lt;p&gt;Our controller is like the brains of the operation, it's connecting our view (and the React components within) with the model and each instance of place. In Rails, the index action is where we access all of the instances of a class. In this case our class is Place and our instances are the 5 locations we seeded earlier (e.g. The White House).&lt;/p&gt;




&lt;h3&gt;
  
  
  3.1. Showing our places
&lt;/h3&gt;




&lt;p&gt;The first thing we need to do is make sure our index page can read our places. Go back to &lt;code&gt;places_controller.rb&lt;/code&gt; and add this line of code in the &lt;code&gt;index&lt;/code&gt; action:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def index
  @places = Place.where.not(latitude: nil, longitude: nil)
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our view when we want to access our places data we can call &lt;code&gt;@places&lt;/code&gt; and it should return every instance. Using &lt;code&gt;.where.not(latitude: nil, longitude: nil)&lt;/code&gt; reads almost as we would say it in layman's terms - we only want places where the coordinates are not nil, or empty. If they're empty they'll be excluded from the results since we need coordinates for our map markers.&lt;/p&gt;

&lt;p&gt;For some visual feedback, let's quickly add a few simple &lt;code&gt;erb&lt;/code&gt; and HTML elements to our &lt;code&gt;index.html.erb&lt;/code&gt; page. Underneath the map component, add the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;% @places.each do |place| %&amp;gt;
  &amp;lt;h2&amp;gt;&amp;lt;%= place.name %&amp;gt;&amp;lt;/h2&amp;gt;
  &amp;lt;p&amp;gt;&amp;lt;%= place.address %&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;% end %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we use an each loop to iterate over our places and create an H2 and paragraph with the name and address respectively. It should look something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpr2t0lmql54no2s6yslu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpr2t0lmql54no2s6yslu.png" alt="Map and places"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  3.2. Sorting our controller logic
&lt;/h3&gt;




&lt;p&gt;We can access whatever information we pass in the index action of our controller. At the moment our default center is sitting over Fortaleza, Brazil but we want to make this dynamic. Thankfully props in React play well with Rails to allow us to pass conditional data which means, depending on how many places we have or where they are, we can change where our map drops us.&lt;/p&gt;

&lt;p&gt;In our &lt;code&gt;index.html.erb&lt;/code&gt; file where we call our Map component let's add a few properties:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;%= react_component("Map", {
  center: @map_center,
  zoom: @map_zoom,
  markers: [@markers]
}) %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Of course these properties don't actually exist yet so let's add them to our controller. Let's start with markers. In the React component above we pass them as an array - this is so that we can iterate over them to create individual markers.&lt;/p&gt;

&lt;h4&gt;
  
  
  Markers
&lt;/h4&gt;

&lt;p&gt;In our controller's index action we'll do some more geocoding:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    @markers = @places.geocoded.map do |place|
      {
        id: place.id,
        lat: place.latitude,
        lng: place.longitude,
        name: place.name,
        address: place.address
      }
    end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The important information we need from here are the coordinates (lat, lng) and the id (for unique keys in React). I've added the name and address for future Google Maps InfoWindow components but we can ignore these for now.&lt;/p&gt;

&lt;h4&gt;
  
  
  Center and zoom
&lt;/h4&gt;

&lt;p&gt;@map_center and @map_zoom are a little bit more complicated as we'll want these to change based on certain criteria. For example if there are no markers to display, we need a default place to land on. If we have only one place we want our map to center on that spot, and if we have more than one maybe we want a general area encompassing all our places. That's a lot of ifs that we can turn into a conditional:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if @places.count.zero?
  @map_center = [38.9072, 77.0369] # Washington D.C.
  @map_zoom = 0
elsif @places.count == 1
  @map_center = [@places[0].latitude, @places[0].longitude]
  @map_zoom = 14
else
  avg_lat = 0
  avg_lon = 0

  @places.map do |place|
    avg_lat += place.latitude
    avg_lon += place.longitude
  end

  @map_center = [(avg_lat / @places.count), (avg_lon / @places.count)]
  @map_zoom = 12
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's a lot to take in so let's break it down into bite-sized pieces:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if @places.count.zero?
  @map_center = [38.9072, 77.0369] # Washington D.C.
  @map_zoom = 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we're saying that if there are no places to add to the map, set our default center to the middle of Washington D.C. I did this because our markers are all based there but you can change these defaults to wherever you like. For zoom, the higher the number, the closer the zoom.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;elsif @places.count == 1
  @map_center = [@places[0].latitude, @places[0].longitude]
  @map_zoom = 14
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same idea as above except this time we're checking if there's only one place. If so, we want our map to be centered on that place's coordinates (&lt;code&gt;[@places[0].latitude, @places[0].longitude]&lt;/code&gt;) and zoomed in on a closer area.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;else
  avg_lat = 0
  avg_lon = 0

  @places.map do |place|
    avg_lat += place.latitude
    avg_lon += place.longitude
  end

  @map_center = [(avg_lat / @places.count), (avg_lon / @places.count)]
  @map_zoom = 12
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we have more than one place then we want to show all the markers on the screen. To do this we define two variables (&lt;code&gt;avg_lat&lt;/code&gt; and &lt;code&gt;avg_lon&lt;/code&gt;) and use these to total up the coordinates of each place. We then divide each one by the number of places we have to give us a middle point.&lt;/p&gt;

&lt;p&gt;This solution works for a city-sized area but if you're planning to have places across multiple cities or even countries then using &lt;code&gt;fitbounds()&lt;/code&gt; might yield better results. This will require a bit more digging into React as you'll need to tap into the power of hooks (especially &lt;a href="https://reactjs.org/docs/hooks-effect.html" rel="noopener noreferrer"&gt;useEffect()&lt;/a&gt;).&lt;/p&gt;




&lt;h3&gt;
  
  
  3.3. Adding props to our Map component
&lt;/h3&gt;




&lt;p&gt;Next, go to &lt;code&gt;Map.js&lt;/code&gt; in our &lt;code&gt;app/javascript/components&lt;/code&gt; folder. Here we need to add the ability for our component to access &lt;code&gt;props&lt;/code&gt;. In the function declaration add the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Map = (props) =&amp;gt; {
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By passing props in the declaration we now have access to whatever data we feed the component when it's rendered. In our &lt;code&gt;index.html.erb&lt;/code&gt; we provided &lt;code&gt;center&lt;/code&gt;, &lt;code&gt;zoom&lt;/code&gt;, and &lt;code&gt;markers&lt;/code&gt;, so in our component we can access these by simply adding &lt;code&gt;props.&lt;/code&gt; before the name. In the body of our Map function, let's make a few changes.&lt;/p&gt;

&lt;p&gt;First Let's see what data we're actually working with. Inside the body of our Map component, let's do a quick log to console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Map = (props) =&amp;gt; {
  console.log(props)
  ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which will return this in our browser console:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkpvuzywial28d6u7b04c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkpvuzywial28d6u7b04c.png" alt="Props console"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Think of the parent &lt;code&gt;Object&lt;/code&gt; as the props in our &lt;code&gt;props.{data}&lt;/code&gt; call. If we call &lt;code&gt;props.center&lt;/code&gt; we'll get an array with two elements - our latitude and longitude. They aren't labeled as such but this is what we passed earlier in our controller as &lt;code&gt;@map_center&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can only access props within the body of the function so let's move our &lt;code&gt;center&lt;/code&gt; variable inside and give it some props:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Map = (props) =&amp;gt; {
  const center = {
    lat: props.center[0],
    lng: props.center[1]
  };

  return (
    &amp;lt;GoogleMap
      mapContainerStyle={containerStyle}
      center={center}
      zoom={10}
    &amp;gt;
      { /* Child components, such as markers, info windows, etc. */ }
      &amp;lt;&amp;gt;&amp;lt;/&amp;gt;
    &amp;lt;/GoogleMap&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Refresh your page and you should see Washington D.C. (or your place of choice):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbj4ceonqzmmd6sxccv4f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbj4ceonqzmmd6sxccv4f.png" alt="Map of Washington D.C. zoom 10"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We now have a dynamic center point based on the conditions we set out in our index action! Next, let's set some props for our zoom property:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;GoogleMap
      mapContainerStyle={containerStyle}
      center={center}
      zoom={props.zoom}
    &amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now our map should be more focused on a general area:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F05pwvpct4inkco6tiw5m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F05pwvpct4inkco6tiw5m.png" alt="Map of Washington D.C. dynamic zoom"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;h2&gt;
  
  
  4. Making markers for our places
&lt;/h2&gt;




&lt;p&gt;Our map is still missing a key part - markers. We have a general idea of where we are thanks to our new default center and zoom, but we have no idea where to look. Here we're going to make use of the Marker and InfoWindow components provided to us by &lt;code&gt;react-google-maps&lt;/code&gt;. Let's start by importing them at the top of our Map component. Where we import GoogleMap, replace that line with the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {
  GoogleMap,
  Marker,
  InfoWindow,
} from '@react-google-maps/api';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, inside of our &lt;code&gt;GoogleMap&lt;/code&gt; component within the function let's add our markers. If we revisit the data we logged in the console earlier, you'll see that markers were provided as an array:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvrzduuudakw9jz3y8bna.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvrzduuudakw9jz3y8bna.png" alt="Markers array"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This data comes from &lt;code&gt;@markers&lt;/code&gt; in our controller. A powerful way of creating multiple components easily is to use JavaScript's &lt;code&gt;.map&lt;/code&gt; method. Inside the &lt;code&gt;GoogleMap&lt;/code&gt; component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;GoogleMap
  mapContainerStyle={containerStyle}
  center={center}
  zoom={props.zoom}
&amp;gt;
  {props.markers[0].map(marker =&amp;gt; 
    &amp;lt;Marker
      key={marker.id}
      animation={2}
      position={{
        lat: marker.lat,
        lng: marker.lng,
      }}
    /&amp;gt;
  )}
&amp;lt;/GoogleMap&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because &lt;code&gt;markers&lt;/code&gt; is an array of arrays, we need to select the element at the first index to map over. From there, we create a new &lt;code&gt;Marker&lt;/code&gt; component for each marker element. &lt;/p&gt;

&lt;p&gt;When creating multiple components from an array we need to provide React with unique keys to differentiate them. In this case we have a built-in &lt;code&gt;id&lt;/code&gt; that will always be unique thanks to the way our PostgreSQL database works. We then provide the position as a JavaScript object in the same way that we created the &lt;code&gt;center&lt;/code&gt; variable earlier, only this time it's done in-line.&lt;/p&gt;

&lt;p&gt;We should finally have some working markers on our map. Let's check it out. Refresh your page and you should see this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F98wq9xigf0sy102vvywn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F98wq9xigf0sy102vvywn.png" alt="Map with markers"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Awesome! We now have dynamic markers that will update each time you add or remove a place from your database, along with your maps zoom and center point. All that's left to do is add an &lt;code&gt;InfoWindow&lt;/code&gt; component to our markers to display some information when we click on them.&lt;/p&gt;



&lt;h2&gt;
  
  
  5. Adding infowindows to markers
&lt;/h2&gt;




&lt;p&gt;At the moment clicking on our markers yields no results which is terrible for user experience. Instead maybe we'd like to show the name of the place, the address, or any other information we make available.&lt;/p&gt;

&lt;p&gt;We have to do a bit of extra work before we can implement these. First, we're going to make use of the &lt;a href="https://reactjs.org/docs/hooks-state.html" rel="noopener noreferrer"&gt;useState&lt;/a&gt; hook due to the fact that we've defined Map as a functional component. First, let's import &lt;code&gt;useState&lt;/code&gt; at the top of our component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from 'react'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The reason we have to use this hook is because we need to be able to determine which marker we've clicked on in order to display the correct infowindow. To do this we'll create a state called &lt;code&gt;selected&lt;/code&gt;. Inside our Map function, add the following at the top:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Map = (props) =&amp;gt; {
  const [selected, setSelected] = useState(null)
  ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We'll use this state together with an &lt;code&gt;onClick&lt;/code&gt; function to tell React which marker we've clicked on. To do this, we need to add another property to the Marker components rendered from our &lt;code&gt;.map&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{props.markers[0].map(marker =&amp;gt; 
  &amp;lt;Marker
    key={marker.id}
    animation={2}
    position={{
      lat: marker.lat,
      lng: marker.lng,
    }}
    onClick={() =&amp;gt; {
      setSelected(marker)
    }}
  /&amp;gt;
)}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nothing's happening yet because we're not telling React what to do when the state changes. To do so, add this underneath your markers, before the closing tag of the &lt;code&gt;GoogleMap&lt;/code&gt; component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{selected ? 
  (&amp;lt;InfoWindow 
      position={{ lat: selected.lat, lng: selected.lng }}
      onCloseClick={() =&amp;gt; {
        setSelected(null)
      }}
    &amp;gt;
    &amp;lt;div style={{ maxWidth: 120 }}&amp;gt;
      &amp;lt;p&amp;gt;{ selected.name }&amp;lt;/p&amp;gt;
      &amp;lt;small&amp;gt;{ selected.address }&amp;lt;/small&amp;gt;
    &amp;lt;/div&amp;gt;
  &amp;lt;/InfoWindow&amp;gt;) : null
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we're using a ternary operator to do a bit of conditional rendering for us. If the marker is selected (by clicking on it), it'll render an &lt;code&gt;InfoWindow&lt;/code&gt; component that shows us the name and address of the chosen place. When we click the close button, it sets the selected state back to &lt;code&gt;null&lt;/code&gt; in this line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;onCloseClick={() =&amp;gt; {
  setSelected(null)
}}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the marker doesn't match the selected one, nothing is rendered. If you refresh your page, when you click on any marker you should see a small window appear:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsph9dcbyj4mgzcnj8mx7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsph9dcbyj4mgzcnj8mx7.png" alt="Marker infowindow"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;h2&gt;
  
  
  6. Next steps
&lt;/h2&gt;




&lt;p&gt;And there you have it, we now have a working map rendering markers and infowindows for each place we create. Where you go from here is entirely up to your imagination - this could be used to display branches of a company, cafes in a city, or tap into a location-based API to display events in an area.&lt;/p&gt;

&lt;p&gt;If you plan to host your project online, make sure that you import your environment variables into whichever platform you choose as these will replace the data in the &lt;code&gt;.env&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;Almost every aspect of the map can be customised, from &lt;a href="https://snazzymaps.com/" rel="noopener noreferrer"&gt;the color of the map&lt;/a&gt; to the marker icons and infowindow contents like these from my final project at Le Wagon:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsr8pjmk36ur6100hqg6d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsr8pjmk36ur6100hqg6d.png" alt="Safe Spaces infowindow"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Feel free to clone the project and make it your own and adapt it. Share your thoughts and ideas in the comments!&lt;/p&gt;

&lt;p&gt;Thank you for following along!&lt;/p&gt;



&lt;h2&gt;
  
  
  7. Get in touch
&lt;/h2&gt;

&lt;p&gt;If you want to see more of my work, feel free to reach out through any of these platforms:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.rhysmalyon.com" rel="noopener noreferrer"&gt;Portfolio&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.github.com/RhysMalyon" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.linkedin.com/in/rhysmalyon" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>googlemaps</category>
      <category>react</category>
      <category>rails</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Setting up Google Maps API</title>
      <dc:creator>Rhys Malyon</dc:creator>
      <pubDate>Fri, 12 Nov 2021 14:38:11 +0000</pubDate>
      <link>https://dev.to/rhysmalyon/setting-up-google-maps-api-1ji6</link>
      <guid>https://dev.to/rhysmalyon/setting-up-google-maps-api-1ji6</guid>
      <description>&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;




&lt;ol&gt;
&lt;li&gt;Google Maps API keys&lt;/li&gt;
&lt;li&gt;Creating API keys&lt;/li&gt;
&lt;li&gt;Choosing our APIs&lt;/li&gt;
&lt;li&gt;API keys and .env&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  1. Google Maps API keys
&lt;/h2&gt;




&lt;h3&gt;
  
  
  Disclaimer
&lt;/h3&gt;




&lt;p&gt;In order to create a Google Developer account, you'll need to have a credit card on hand. Don't worry, Google are kind enough to provide $200 of free credit each month, which is more than enough for what we're doing here. Just be aware though that with each load of the map and call to various APIs those credits will be charged meaning the bigger your app gets, the higher the likelihood that you'll have to pay for usage.&lt;/p&gt;




&lt;p&gt;Before we jump into creating our map component, there's a few things we need to do. Google Maps is a feature-rich API with a whole lot of options for developers to tap into but you need a unique key to be able to make calls to their API. It's a fairly straightforward process which I'll walk you through.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Creating API keys
&lt;/h2&gt;




&lt;p&gt;First, make sure you're logged in to a valid Google account - you'll need it to get access to the tools we'll need. Next, head to the &lt;a href="https://console.cloud.google.com/apis/dashboard" rel="noopener noreferrer"&gt;Google Cloud Platform dashboard&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fghxbx470ilm8xeochdp7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fghxbx470ilm8xeochdp7.png" alt="Google Cloud Platform dashboard"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;API keys are unique which means that for each new application or project you create, you also have to make a new project from this platform. In the navigation bar you can create a new project or choose an existing one:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgq0aqd5qgme1gnrtzvui.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgq0aqd5qgme1gnrtzvui.png" alt="Navbar project"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once your project has been created go to the credentials tab on the left navigation and click &lt;code&gt;CREATE CREDENTIALS&lt;/code&gt; at the top of the page:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foi92bd8cci1mswrze9xu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foi92bd8cci1mswrze9xu.png" alt="Create API key"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Do this process twice. For the first key, don't worry about restricting it, just click &lt;code&gt;CLOSE&lt;/code&gt;. For the second key we'll need to add some restrictions as we'll be using this later in a way that will display it in the browser.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fap46xwxprvp55t94mv8j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fap46xwxprvp55t94mv8j.png" alt="Restrict API key"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On the next page, choose &lt;code&gt;HTTP referrers (web sites)&lt;/code&gt; as the application restriction and add click &lt;code&gt;ADD AN ITEM&lt;/code&gt;. This input allows us to choose which websites have access to the key - in this case we need to set &lt;code&gt;localhost:3000/*&lt;/code&gt; to give access to our local development environment. If you plan to push this project to a live repository, add the link to the hosted site in here as well.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2i62s5ygdrzrgrcnaoob.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2i62s5ygdrzrgrcnaoob.png" alt="Add restrictions"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Leave API restrictions at the bottom for now, we'll only add a select group of Google APIs next.&lt;/p&gt;

&lt;p&gt;The final step of our API key setup is to give them unique names as they'll each be used for different purposes (server and browser). The restricted key will be used for the browser so you could name them as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqpzjl8p9bi7a8asq0srs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqpzjl8p9bi7a8asq0srs.png" alt="Edit API key names"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Choosing our APIs
&lt;/h2&gt;




&lt;p&gt;Google Maps has a lot of APIs on offer that can give us all of the functionality that the product itself has to offer built into our own projects. You can add as many as you like but for our core functionality we'll only be using two:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Maps JavaScript API&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Add a map to your website, providing imagery and local data from the same source as Google Maps. Style the map to suit your needs. Visualize your own data on the map, bring the world to life with Street View, and use services like geocoding and directions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Geocoding API&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Convert addresses into geographic coordinates (geocoding), which you can use to place markers or position the map. This API also allows you to convert geographic coordinates into an address (reverse geocoding).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In the side navigation click on &lt;code&gt;Library&lt;/code&gt;. This will take you to a new page where you can search for the APIs:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5jy6cn3p262xmrci7qxh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5jy6cn3p262xmrci7qxh.png" alt="Library search"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the search box, enter &lt;code&gt;Maps JavaScript API&lt;/code&gt; and open the top result. On the API page, click &lt;code&gt;ENABLE&lt;/code&gt;. You'll be redirected to an API dashboard that shows which APIs you have enabled. Go back to the library page and repeat the same process for Geocoding API. &lt;/p&gt;

&lt;p&gt;Now we're all set to put our keys into our project!&lt;/p&gt;




&lt;h2&gt;
  
  
  4. API keys and .env
&lt;/h2&gt;




&lt;p&gt;API keys are considered sensitive data. Remember when I mentioned that Google charges for each call of their APIs? Well imagine if someone got hold of your key and used it for their own purposes - you might wake up to an unpleasant surprise bill from Google at the end of the month.&lt;/p&gt;

&lt;p&gt;To prevent your keys, or any other sensitive data in your project, from being accessible where it shouldn't you'll need what we call environment variables. These are a way for you to reference this data from anywhere in your project without exposing it at each point. It's both safe and convenient!&lt;/p&gt;

&lt;p&gt;Before we go ahead and create our &lt;code&gt;.env&lt;/code&gt; file, we'll need to do a bit of setting up to make it readable in our development environment. We'll be using the &lt;a href="https://github.com/bkeepers/dotenv" rel="noopener noreferrer"&gt;dotenv&lt;/a&gt; gem. We only want to use this locally so let's go back to our &lt;code&gt;Gemfile&lt;/code&gt;, find the development and test group, and add the gem there. Replace it with the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  gem 'dotenv-rails'
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, close your server if it's running (&lt;code&gt;Ctrl+C&lt;/code&gt;) and run the Rails gem install command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bundle install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should have a long list of installed gems - no need to read through them so long as there aren't any errors! Now our local environment is set up and ready to read environment variables.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sidenote on environment variables
&lt;/h3&gt;

&lt;p&gt;In Rails there are gems such as &lt;code&gt;dotenv&lt;/code&gt; available, as well as other technical ways of managing environment variables as showcased in &lt;a href="https://www.youtube.com/watch?v=KRzt_vTZaLQ" rel="noopener noreferrer"&gt;this video&lt;/a&gt;. For the sake of simplicity we'll create our own for now but, if you're working on bigger projects or particularly sensitive data then it's worth exploring other options to find the right one for your use case.&lt;/p&gt;



&lt;h3&gt;
  
  
  Creating our .env file
&lt;/h3&gt;




&lt;p&gt;This part is fairly simple. In the root directory of your project (&lt;code&gt;/rails-react-google-maps&lt;/code&gt;) create a new file called &lt;code&gt;.env&lt;/code&gt; (it mustn't have a file extension on the end). &lt;/p&gt;

&lt;p&gt;Before we place anything in it we need to make sure it won't be shared if we push to any online repository (e.g. GitHub). In your root folder should be a file named &lt;code&gt;.gitignore&lt;/code&gt; - open it up and add the following at the bottom:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you did this correctly the name of the file should now be greyed out in your code editor's file explorer.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa79q4qhg8472sd1utz1n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa79q4qhg8472sd1utz1n.png" alt="Ignored .env"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;h3&gt;
  
  
  Saving our keys as environment variables
&lt;/h3&gt;




&lt;p&gt;All that's left to do is add our Google Maps API keys to the &lt;code&gt;.env&lt;/code&gt; file to make them accessible throughout our app. Naming conventions for environment variables are as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;NAME_OF_VARIABLE=value
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So if we add our keys with descriptive names it'd look something like this (replace [your_key_here] with your own API keys):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GMAPS_API_SERVER_KEY=[your_key_here]
GMAPS_API_BROWSER_KEY=[your_key_here]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then when you want to access these values in your app you call them using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ENV['GMAPS_API_BROWSER_KEY']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just like that we have secure environment variables which, along with the key restrictions on your browser key, should keep your sensitive API keys safe from prying eyes!&lt;/p&gt;




&lt;p&gt;With that out of the way we're ready for the last step - building our map component! We'll be exploring our controller again and revisiting React components and props in the next step.&lt;/p&gt;

</description>
      <category>rails</category>
      <category>react</category>
      <category>postgres</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Installing React in Rails and creating your first component</title>
      <dc:creator>Rhys Malyon</dc:creator>
      <pubDate>Fri, 12 Nov 2021 14:38:02 +0000</pubDate>
      <link>https://dev.to/rhysmalyon/installing-react-and-creating-our-first-component-1e1c</link>
      <guid>https://dev.to/rhysmalyon/installing-react-and-creating-our-first-component-1e1c</guid>
      <description>&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;




&lt;ol&gt;
&lt;li&gt;Installing React&lt;/li&gt;
&lt;li&gt;Rendering our first component&lt;/li&gt;
&lt;li&gt;Exploring our React component&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  1. Installing React
&lt;/h2&gt;




&lt;p&gt;With our app up and running it's time to get to the nitty gritty of implementing React into our project. If you're not too familiar with React, it's a JavaScript framework maintained by Facebook that's taken the world of front end development by storm in the last few years.&lt;/p&gt;

&lt;p&gt;If you want to learn more about React check out their &lt;a href="https://reactjs.org/"&gt;official site&lt;/a&gt; or work through their &lt;a href="https://reactjs.org/tutorial/tutorial.html"&gt;tutorial&lt;/a&gt;. I'm going to continue under the assumption that you have at least a basic understanding of how React works but I will explore some key ideas that we'll be using along the way.&lt;/p&gt;




&lt;h3&gt;
  
  
  React-Rails
&lt;/h3&gt;




&lt;p&gt;We'll be using the &lt;a href="https://github.com/reactjs/react-rails"&gt;React-Rails&lt;/a&gt; gem to integrate React functionality into our app. The process for adding the gem is the same as we did for &lt;code&gt;geocoder&lt;/code&gt; - open your Gemfile in your project's root folder and add the following line underneath the geocoder gem:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gem 'geocoder'
gem 'react-rails'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we need to run the installer. You'll need to run a few commands to get everything running - these are taken straight from the documentation installation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bundle install
rails webpacker:install
rails webpacker:install:react
rails generate react:install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These commands will create a bunch of files but the most important are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;app/javascript/components/&lt;/code&gt; directory for your React components&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ReactRailsUJS&lt;/code&gt; setup in &lt;code&gt;app/javascript/packs/application.js&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;app/javascript/packs/server_rendering.js&lt;/code&gt; for server-side rendering&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The installation instructions ask us to add &lt;code&gt;&amp;lt;%= javascript_pack_tag 'application' %&amp;gt;&lt;/code&gt; to the head of our &lt;code&gt;application.html.erb&lt;/code&gt; file but this isn't necessary as this is already included as part of the turbolinks import in the same place.&lt;/p&gt;



&lt;h2&gt;
  
  
  2. Rendering our first component
&lt;/h2&gt;




&lt;p&gt;A hallmark of React is its focus on creating modular interfaces with reusable components. Build a component once and you can render it as many times as you want. We'll be using a few small components to build a basic UI for our app but first we need to make sure we can actually render a component in our view.&lt;/p&gt;

&lt;p&gt;First let's follow the React-Rails tutorial to build a simple hello world greeting. It's a quick and easy way to check that everything is working before we start building our map component.&lt;/p&gt;

&lt;p&gt;To create a React component in Rails we can use the generate command again. Instead of typing out &lt;code&gt;generate&lt;/code&gt; each time, we can shorten it to &lt;code&gt;g&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails g react:component HelloWorld greeting:string
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see that a file was created:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Running via Spring preloader in process 440408
      create  app/javascript/components/HelloWorld.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means we have our first component! Now we need to connect it to our view to check that everything is working. For this we need to go to our &lt;code&gt;/rails-react-google-maps/app/views/places/index.html.erb&lt;/code&gt; file again and replace everything in there with the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;%= react_component("HelloWorld", { greeting: "Hello from react-rails." }) %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If your rails server is still running, close it (Ctrl+C) and re-open it (&lt;code&gt;rails s&lt;/code&gt;). Go back to &lt;code&gt;localhost:3000&lt;/code&gt; and you should have a simple sentence on your page.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rt6Ya2_4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/np50q95masfclunu3sn4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rt6Ya2_4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/np50q95masfclunu3sn4.png" alt="Hello World component" width="277" height="33"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our first React component is rendering in our view!&lt;/p&gt;



&lt;h2&gt;
  
  
  3. Exploring our React component
&lt;/h2&gt;




&lt;p&gt;Let's have a quick look at what we created with the component generator. Navigate to &lt;code&gt;/rails-react-google-maps/app/javascript/components/HelloWorld.js&lt;/code&gt; in your code editor:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react"
import PropTypes from "prop-types"
class HelloWorld extends React.Component {
  render () {
    return (
      &amp;lt;React.Fragment&amp;gt;
        Greeting: {this.props.greeting}
      &amp;lt;/React.Fragment&amp;gt;
    );
  }
}

HelloWorld.propTypes = {
  greeting: PropTypes.string
};
export default HelloWorld
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I won't go into the details of how a React component is built or the difference between class-based and functional components (&lt;a href="https://youtu.be/JxzzpDHYYXk"&gt;this video&lt;/a&gt; does a great job of explaining), but what I want to draw your attention to is the &lt;code&gt;props&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;React gives us the ability to pass properties, or props, from a parent component to its children. In our app, the &lt;code&gt;application.html.erb&lt;/code&gt; represents the parent which is rendering our HelloWorld component. When we render the component like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;%= react_component("HelloWorld", { greeting: "Hello from react-rails." }) %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We're passing it a greeting string. If we dig into our component, you'll see this line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Greeting: {this.props.greeting}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On our index page we're shown &lt;code&gt;Greeting: Hello from react-rails.&lt;/code&gt; because we're telling the HelloWorld component that we want that greeting property (&lt;code&gt;this.props.greeting&lt;/code&gt;) to be equal to &lt;code&gt;Hello from react-rails.&lt;/code&gt;. This gives us a lot of flexibility and customization when building and rendering components and this will play a key role in displaying data on our future map component.&lt;/p&gt;

&lt;p&gt;Give it a try, in your &lt;code&gt;application.html.erb&lt;/code&gt; add another HelloWorld component and pass your own string! If you want to learn more about how props work, check out the &lt;a href="https://reactjs.org/docs/components-and-props.html"&gt;Components and Props documentation&lt;/a&gt; from the React team.&lt;/p&gt;




&lt;p&gt;Now that we're connected to React and we know how to make components, it's time to move on to the main feature of our app - the map!&lt;/p&gt;

</description>
      <category>rails</category>
      <category>react</category>
      <category>postgres</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Building a Rails app backbone</title>
      <dc:creator>Rhys Malyon</dc:creator>
      <pubDate>Fri, 12 Nov 2021 14:37:49 +0000</pubDate>
      <link>https://dev.to/rhysmalyon/building-our-rails-backbone-2jmb</link>
      <guid>https://dev.to/rhysmalyon/building-our-rails-backbone-2jmb</guid>
      <description>&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;




&lt;ol&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;Background&lt;/li&gt;
&lt;li&gt;Prerequisites&lt;/li&gt;
&lt;li&gt;Setting up your app&lt;/li&gt;
&lt;li&gt;Building our database&lt;/li&gt;
&lt;li&gt;Creating our Place model&lt;/li&gt;
&lt;li&gt;Geocoder gem&lt;/li&gt;
&lt;li&gt;Seeding our database&lt;/li&gt;
&lt;li&gt;Generating our views and controllers&lt;/li&gt;
&lt;li&gt;Routing&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  1. Introduction
&lt;/h2&gt;




&lt;p&gt;Maps are a useful weapon for any developer to have in their arsenal and, with the combined dev-friendly approach to Rails and massive community built around React, now is the perfect time to use these two technologies together to add another layer to your project.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Background
&lt;/h2&gt;




&lt;p&gt;This tutorial was largely inspired by my time at &lt;a href="https://www.lewagon.com/" rel="noopener noreferrer"&gt;Le Wagon&lt;/a&gt; in Tokyo. Over the course of 9 weeks we dived into a lot of core concepts of full-stack web development using Ruby and the Rails framework. I'd fully recommend it if you're interested in fast-tracking your development learning progress or looking to get into the world of Ruby development.&lt;/p&gt;

&lt;p&gt;We touched on JavaScript during the course and used it pretty extensively in our final project, &lt;a href="https://github.com/RhysMalyon/safe-spaces" rel="noopener noreferrer"&gt;Safe Spaces&lt;/a&gt;, but with the main focus being on Rails we didn't have any time to explore some of the popular JS frameworks.&lt;/p&gt;

&lt;p&gt;After the end of Le Wagon I wanted to get some hands-on experience with React and ended up working on a small side project that involved integrating Google Maps into a Rails project with the &lt;a href="https://www.npmjs.com/package/@react-google-maps/api" rel="noopener noreferrer"&gt;@react-google-maps/api&lt;/a&gt; package. I decided to create this tutorial to share my experience in building a simple starter using these tools.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Prerequisites
&lt;/h2&gt;




&lt;p&gt;To follow along with this article you'll need at least a basic understanding of Ruby on Rails, MVC (Model View Controller), JavaScript, and React. You'll also need a Google account in order to create a Google Maps API key and a code editor of your choice (e.g. Visual Studio Code).&lt;/p&gt;

&lt;p&gt;This tutorial also requires you to have the following installed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.ruby-lang.org/en/documentation/installation/" rel="noopener noreferrer"&gt;Ruby&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://guides.rubyonrails.org/v5.0/getting_started.html" rel="noopener noreferrer"&gt;Rails&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.npmjs.com/downloading-and-installing-node-js-and-npm" rel="noopener noreferrer"&gt;Node.js / npm&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.postgresql.org/download/" rel="noopener noreferrer"&gt;PostgreSQL&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This tutorial was tested on a fresh Ubuntu 20.04 install following the installation instructions provided by the docs in each prerequisite. If you have issues at any point feel free to drop a comment or reach out via DM.&lt;/p&gt;

&lt;p&gt;If you get stuck at any point, feel free to reference the &lt;a href="https://github.com/RhysMalyon/rails-react-google-maps" rel="noopener noreferrer"&gt;source code&lt;/a&gt; or clone the project and follow along.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Setting up your app
&lt;/h2&gt;




&lt;p&gt;We're going to be building our project using a Rails backbone connected to a PostgreSQL database. If you have all the prerequisites installed, generate a new app with the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails new rails-react-google-maps --database=postgresql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a new folder in your current directory with the same name as your app. Rails uses SQLite3 as its default database so by passing the &lt;code&gt;--database=postgresql&lt;/code&gt; option in the setup we tell the app to use PostgreSQL instead to save some time.&lt;/p&gt;




&lt;h3&gt;
  
  
  PostgreSQL database management in Rails
&lt;/h3&gt;




&lt;p&gt;Now that we have our basic structure set up we can get started with creating the database we'll use to hold all of our Place data. Rails makes database management easy with a few simple commands. The ones we'll be using in this tutorial are:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails db:create
rails db:migrate
rails db:seed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;rails db:create&lt;/code&gt; does what it says on the box - creates our database. This is an important step as without it we'll get an error when we try to run our server.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails db:migrate&lt;/code&gt; is something we'll touch on soon but it's a part of that "rails magic" that makes database management simple. I'd recommend reading the &lt;a href="https://edgeguides.rubyonrails.org/active_record_migrations.html" rel="noopener noreferrer"&gt;Rails Guide on Active Record Migrations&lt;/a&gt; to learn more about how they work.&lt;/p&gt;

&lt;p&gt;In simple terms, migrations allow us to manipulate the tables in our database - whether they're adding or removing columns, changing data types, or renaming tables themselves - which can all be done from the command line.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails db:seed&lt;/code&gt; will mainly be used in the development phase of our project to create starter data we'll use to populate our app. We'll explore this more when we start creating instances of our Place model.&lt;/p&gt;

&lt;p&gt;To learn more about what other commands you have at your disposal, check out the &lt;a href="https://guides.rubyonrails.org/active_record_migrations.html#running-migrations" rel="noopener noreferrer"&gt;Running Migrations&lt;/a&gt; section in the previously linked Rails Guide.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. Building our database
&lt;/h2&gt;




&lt;p&gt;To get started, run the following command in your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails db:create
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see this output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Created database 'rails_react_google_maps_development'
Created database 'rails_react_google_maps_test'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We now have our database set up and ready to store our data. To make sure everything is working, type &lt;code&gt;rails server&lt;/code&gt; in your terminal to start up your app. You should see the basic Rails welcome page.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9k60tlip7fspsuk99iv7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9k60tlip7fspsuk99iv7.png" alt="Rails welcome page"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  6. Creating our Place model
&lt;/h2&gt;




&lt;p&gt;Now that our app and database are running we can start creating our Place model that will give us the data we need to display on Google Maps. Let's generate the model in the terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails generate model Place name:string address:string latitude:float longitude:float
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should have something in your terminal that looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Running via Spring preloader in process 387002
      invoke  active_record
      create    db/migrate/20211104052039_create_places.rb
      create    app/models/place.rb
      invoke    test_unit
      create      test/models/place_test.rb
      create      test/fixtures/places.yml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means we've got our Place model up and running! But we're not quite done yet because it's not technically connected to our database. To do this we'll be using the file generated by this line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;create    db/migrate/20211104052039_create_places.rb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember the migrations we spoke about earlier? Well this is where they come into play. To connect our new model to our database all you have to do is run a migration in your terminal and you should get some feedback:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails db:migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;== 20211104052039 CreatePlaces: migrating =====================================
-- create_table(:places)
   -&amp;gt; 0.0106s
== 20211104052039 CreatePlaces: migrated (0.0107s) ============================
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There you have it, we've now created our database, generated a model to create new data, and connected that model to the database.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sidenote about Models and MVC
&lt;/h3&gt;

&lt;p&gt;Rails is built around an MVC (Model View Control) pattern. It's a deeper topic than this tutorial but it's worth looking into if you're interested in exploring what Rails (or any other MVC-based framework) is capable of. &lt;a href="https://medium.com/stackavenue/m-v-c-architecture-of-ruby-on-rails-9712719a69ed" rel="noopener noreferrer"&gt;This Medium article&lt;/a&gt; does a good job of explaining how it relates to Rails directly, while the &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/MVC" rel="noopener noreferrer"&gt;Mozilla docs&lt;/a&gt; on the topic give a more holistic overview.&lt;/p&gt;




&lt;h2&gt;
  
  
  7. Geocoder gem
&lt;/h2&gt;




&lt;p&gt;Before we start making new places we need to install &lt;a href="https://github.com/alexreisner/geocoder" rel="noopener noreferrer"&gt;geocoder&lt;/a&gt;, the gem that will help us get the coordinates of a place from the address we give it. If you're not sure what a gem is, this &lt;a href="https://medium.com/@morgannegagne/what-is-a-ruby-gem-1eec2684e68" rel="noopener noreferrer"&gt;Medium article&lt;/a&gt; gives a pretty good explanation.&lt;/p&gt;

&lt;p&gt;The geocoder gem takes an address and converts it into latitude and longitude - important data that we'll need for Google Maps. We could technically hard code the coordinates when we create a Place but if you're working with more than a handful of places this can get pretty tedious.&lt;/p&gt;

&lt;p&gt;To install geocoder, navigate to your project's root directory (&lt;code&gt;/rails-react-google-maps&lt;/code&gt;) and open the &lt;strong&gt;Gemfile&lt;/strong&gt;. Adding a gem to this file will make it available to anyone who clones your app with one easy command. To find out more about how the Gemfile works, check out the &lt;a href="https://bundler.io/gemfile.html" rel="noopener noreferrer"&gt;documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Add &lt;code&gt;gem 'geocoder'&lt;/code&gt; to your Gemfile anywhere above the development and test groups and save the file. Next, in your terminal run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bundle install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should get a long list of gems with a confirmation message at the bottom. If you scroll through the list you should be able to find &lt;code&gt;Using geocoder 1.6.7&lt;/code&gt;. To create some necessary files for setup, execute the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails generate geocoder:config
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Running via Spring preloader in process 388633
      create  config/initializers/geocoder.rb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we navigate to that geocoder.rb file we can make any changes to the configuration that we want. For example, I use the metric system so instead of the default miles, I can change units to km by uncommenting the units option and changing it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Geocoder.configure(
  ...
  # Calculation options
  units: :km,
  ...
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The final step for setting up our geocoder gem is to point it to the address we want to extract coordinates from. In our project directory, go to the &lt;code&gt;/rails-react-google-maps/app/models&lt;/code&gt; folder and open &lt;code&gt;place.rb&lt;/code&gt; and add the following lines inside the class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  geocoded_by :address
  after_validation :geocode
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first line tells geocoder which part of the model it needs to listen to, in this case the address data we declared when generating the model. This means that whenever we create a place, geocoder will take the address and automatically assign the coordinates to the latitude and longitude without us having to hard code it ourselves.&lt;/p&gt;

&lt;p&gt;To test this out let's open a rails console and create an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails console
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Place.create(address: '1600 Pennsylvania Avenue NW, Washington, DC 20500, United States', name: 'The White House')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If it's successful, you should see a lot of information that looks something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  TRANSACTION (0.3ms)  BEGIN
  Place Create (0.8ms)  INSERT INTO "places" ("name", "address", "latitude", "longitude", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["name", "The White House"], ["address", "1600 Pennsylvania Avenue NW, Washington, DC 20500, United States"], ["latitude", 38.897699700000004], ["longitude", -77.03655315], ["created_at", "2021-11-04 08:22:20.077379"], ["updated_at", "2021-11-04 08:22:20.077379"]]
  TRANSACTION (1.0ms)  COMMIT
 =&amp;gt; #&amp;lt;Place id: 3, name: "The White House", address: "1600 Pennsylvania Avenue NW, Washington, DC 20500,...", latitude: 38.897699700000004, longitude: -77.03655315, created_at: "2021-11-04 08:22:20.077379000 +0000", updated_at: "2021-11-04 08:22:20.077379000 +0000"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you look carefully you'll see that we've got &lt;code&gt;["latitude", 38.897699700000004]&lt;/code&gt; and &lt;code&gt;["longitude", -77.03655315]&lt;/code&gt; which means everything is working!&lt;/p&gt;




&lt;h2&gt;
  
  
  8. Seeding our database
&lt;/h2&gt;




&lt;p&gt;We briefly touched on seeding when we discussed database management in Rails. We've already populated our database with one place (The White House) so now it's time to create a few more places to give us some content to work with.&lt;/p&gt;

&lt;p&gt;To make life easier I've provided a template for our seeds file. Feel free to use these or substitute the addresses for your own. I grabbed these places and addresses off Google Maps so I recommend you do the same and test them in the console if you're unsure.&lt;/p&gt;

&lt;p&gt;Navigate to &lt;code&gt;rails-react-google-maps/db/&lt;/code&gt; and paste the following into your seeds.rb file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;places = [
  {
    name: 'The White House',
    address: '1600 Pennsylvania Avenue NW, Washington, DC 20500, United States'
  },
  {
    name: 'Washington Monument',
    address: '2 15th St NW, Washington, DC 20024, United States'
  },
  {
    name: 'Lincoln Memorial',
    address: '2 Lincoln Memorial Cir NW, Washington, DC 20002, United States'
  },
  {
    name: 'Washington National Cathedral',
    address: '3101 Wisconsin Ave NW, Washington, DC 20016, United States'
  },
  {
    name: 'Ronald Reagan Washington National Airport',
    address: '2401 Smith Blvd, Arlington, VA 22202, United States'
  }
]

puts 'Clearing seeds...'

Place.destroy_all

puts 'Seeds cleared.'

puts 'Seeding the database'

places.each do |place|
  Place.create!(
    name: place[:name],
    address: place[:address]
  )
end

puts "Created #{Place.all.count} places."

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you're not too sure what's happening here, don't worry. All you need to do from here is go back to your terminal and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails db:seed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create an instance for each place, with some feedback in your terminal that should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Clearing seeds...
Seeds cleared.
Seeding the database
Created 5 places.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just to double check, let's revisit the rails console to make sure our places are all set up:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails c
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then let's check that all 5 were generated:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Running via Spring preloader in process 415433
Loading development environment (Rails 6.1.4.1)
2.6.6 :001 &amp;gt; Place.count
   (0.7ms)  SELECT COUNT(*) FROM "places"
 =&amp;gt; 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, let's make sure the first place matches the first seed we created (The White House). You should see this output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;irb(main):002:0&amp;gt; Place.first
  Place Load (0.3ms)  SELECT "places".* FROM "places" ORDER BY "places"."id" ASC LIMIT $1  [["LIMIT", 1]]
=&amp;gt; 
#&amp;lt;Place:0x000056403376b848
 id: 2,
 name: "The White House",
 address: "1600 Pennsylvania Avenue NW, Washington, DC 20500, United States",
 latitude: 38.897699700000004,
 longitude: -77.03655315,
 created_at: Fri, 05 Nov 2021 06:25:00.618439000 UTC +00:00,
 updated_at: Fri, 05 Nov 2021 06:25:00.618439000 UTC +00:00&amp;gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  9. Generating our views and controllers
&lt;/h2&gt;




&lt;p&gt;The next step is for us to generate the views and controllers for our Place model. The &lt;a href="https://guides.rubyonrails.org/layouts_and_rendering.html" rel="noopener noreferrer"&gt;views&lt;/a&gt; are what our users will see in the browser, and it's where we'll be rendering our React components (i.e. the map). The &lt;a href="https://guides.rubyonrails.org/action_controller_overview.html" rel="noopener noreferrer"&gt;controller&lt;/a&gt; is responsible for handling the logic of our app - managing what happens between HTTP requests, routing, and actions. This is a core concept for Rails and I'd strongly suggest studying up on it. Check out &lt;a href="https://guides.rubyonrails.org/routing.html" rel="noopener noreferrer"&gt;the official routing docs&lt;/a&gt; to dive deeper into it.&lt;/p&gt;

&lt;p&gt;We'll use the &lt;code&gt;rails generate&lt;/code&gt; command again to create our controller and views. When generating a controller you can pass it the name of the methods you want to create inside it, which in turn creates the associated views. To get started, head back to your terminal and enter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails generate controller places index
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Running via Spring preloader in process 420964
      create  app/controllers/places_controller.rb
       route  get 'places/index'
      invoke  erb
      create    app/views/places
      create    app/views/places/index.html.erb
      invoke  test_unit
      create    test/controllers/places_controller_test.rb
      invoke  helper
      create    app/helpers/places_helper.rb
      invoke    test_unit
      invoke  assets
      invoke    scss
      create      app/assets/stylesheets/places.scss
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There's a lot to take in so let's break down the important bits of what we just did:&lt;/p&gt;

&lt;h3&gt;
  
  
  Controller &amp;amp; Routes
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;create  app/controllers/places_controller.rb&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;route  get 'places/index'&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here we've created our controller and, because we signaled that we wanted to include an index action, the generator created the route for us. If we check the &lt;code&gt;places_controller.rb&lt;/code&gt; you should see an empty method for our index action that will be revisited once we start rendering in our views:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class PlacesController &amp;lt; ApplicationController
  def index
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Views
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;invoke  erb&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;create    app/views/places&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;create    app/views/places/index.html.erb&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The final piece of the MVC puzzle - this is where all our back end goodness comes to life on the page. Again, thanks to the options we passed in the generate controller command, Rails automatically created two pages for us in the &lt;a href="https://docs.ruby-lang.org/en/2.3.0/ERB.html" rel="noopener noreferrer"&gt;embedded ruby (erb)&lt;/a&gt; format.&lt;/p&gt;

&lt;p&gt;The .erb format is highly flexible and allows us to run Ruby code in our view, making the process of connecting back to front really easy. We'll be using it in-depth later to connect our controller logic to our React components.&lt;/p&gt;




&lt;h2&gt;
  
  
  10. Routing
&lt;/h2&gt;




&lt;p&gt;The last step before we move onto React and Google Maps is to configure our routes. If you're not sure about routing I fully recommend revisiting the routing documentation linked above.&lt;/p&gt;

&lt;p&gt;For now we'll only be using the index route, which returns all instances of a model. If you navigate to &lt;code&gt;rails-react-google-maps/config/routes.rb&lt;/code&gt; you'll see that the controller generator already gave us the route but we'll be cleaning it up using &lt;a href="https://guides.rubyonrails.org/routing.html#resource-routing-the-rails-default" rel="noopener noreferrer"&gt;Rails resources&lt;/a&gt; and specifying what we want to have access to. At the same time we'll reroute the root (homepage) of our app to the index page:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Rails.application.routes.draw do
  root to: 'places#index'
  resources :places, only: %i[index]
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This just moves the routing logic to one line which we can add to or remove from to adjust our routes, as well as making our places index page our new homepage when we start the app.&lt;/p&gt;

&lt;p&gt;Let's test it out. If you're running a server in your terminal, press Ctrl+C to shut it down and restart - if your server isn't running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open &lt;code&gt;localhost:3000&lt;/code&gt; in your browser and you should have a mostly empty page that looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbqp6bwfamzknc6ltiypb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbqp6bwfamzknc6ltiypb.png" alt="Index screenshot"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It's not looking so great at the moment but it means that our front end is connected to our back end!&lt;/p&gt;




&lt;p&gt;Congratulations, you now have a working Rails app that has a working model, a view that's displaying in our browser, and a controller that we'll soon fill with lots of logic goodness to bring our app to life.&lt;/p&gt;

&lt;p&gt;Carry on to Part Two to implement React and use it to build out our map feature in Google Maps!&lt;/p&gt;

</description>
      <category>rails</category>
      <category>react</category>
      <category>postgres</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
