<?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 Ed on Medium]]></title>
        <description><![CDATA[Stories by Ed on Medium]]></description>
        <link>https://medium.com/@edwin.moradian90?source=rss-e02357e7d5c3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*GGpEbstoTCVg5HW4-OR55w.jpeg</url>
            <title>Stories by Ed on Medium</title>
            <link>https://medium.com/@edwin.moradian90?source=rss-e02357e7d5c3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 25 Nov 2026 02:37:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@edwin.moradian90/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[Setting up Rails 6 with React]]></title>
            <link>https://levelup.gitconnected.com/setting-up-rails-6-with-react-4b4d3076545?source=rss-e02357e7d5c3------2</link>
            <guid isPermaLink="false">https://medium.com/p/4b4d3076545</guid>
            <category><![CDATA[rails]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[setup]]></category>
            <category><![CDATA[full-stack]]></category>
            <category><![CDATA[axios]]></category>
            <dc:creator><![CDATA[Ed]]></dc:creator>
            <pubDate>Sun, 19 Apr 2020 22:04:22 GMT</pubDate>
            <atom:updated>2020-04-29T02:37:57.868Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*HTsUd6ilCQ0nGDM7" /><figcaption>Photo by <a href="https://unsplash.com/@seanlimm?utm_source=medium&amp;utm_medium=referral">Sean Lim</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3><strong>Introduction</strong></h3><p>There are many ways to use React as the front-end of your web application. With Rails, there are a couple of gems available that embed React in your Rails code, essentially creating a Rails application with React built-in.</p><p>But what if you want to completely separate concerns, or in other words, not mix your React and Rails code?</p><p>This can be done fairly easily with Rails 6 and will be the topic of interest today.</p><h3>Setting up Rails</h3><p>Conceptually, what we are doing is setting up Rails as an API. This means, from the client-facing application, we will be making API calls to the Rails back-end to retrieve and store our data.</p><p>Rails, out-of-the-box, comes with views, controllers, and tests that are great for a full Rails application, but are not necessary for a Rails API. Instead, we will only require the bare essentials, saving us a good chunk of space in our back-end.</p><p>First, we will create a new folder to house our Rails and React applications and set Rails to its convenient “API mode” by including the API flag during the installation.</p><pre>$ mkdir app &amp;&amp; cd app<br>$ rails new server --api</pre><p>Rails will now create an API-only application, which will exclude all unnecessary files.</p><p>After Rails completes the installation, we will run the server on a new port, since our React application will be using the normal port 3000. For convenience, I’ve used port 3001.</p><pre>$ cd server<br>$ rails server -p 3001</pre><p>Once the application is up and running, we will check that everything is working as expected by navigating to our local host in our browser.</p><pre><a href="https://localhost/">https://localhost:3001</a></pre><p>If everything went smoothly, you should see the Rails welcome screen in your browser.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*oQnNIpv8ZLRfu8wS7shgvQ.png" /><figcaption>Rails Welcome Screen</figcaption></figure><h3>API Versioning</h3><p>Although not necessary, it is highly recommended to track the API version by creating a custom name space for all current and future versions.</p><p>This can be done creating a new name space in your routes.rb file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/683f859f7e4c37836697ce129d55f896/href">https://medium.com/media/683f859f7e4c37836697ce129d55f896/href</a></iframe><p>Here we name space the API and its current version, as to not confuse any future versions and implementations.</p><p>So now, we access our controllers by creating a folder structure similar to our name spaced API.</p><pre>app &gt; controllers &gt; api &gt; v1 &gt; your_model</pre><p>It’s very important that folder structure of our application matches the name spacing we’ve created, or else it will not work properly.</p><h3>Setting up React</h3><p>Now that we’ve set up our Rails API properly, we are ready to include our the client-facing portion of our application with React.</p><p>We will change directories back to our app folder and create a new React application.</p><pre>$ cd ..<br>$ npx create-react-app client</pre><p>Once the installation is complete, we check if everything went smoothly by changing directories into our new client folder and starting up the React development server.</p><pre>$ cd client<br>$ npm start</pre><p>Next, we head over to our browser to make sure that React is working.</p><pre>https://localhost:3000</pre><p>If everything is in order, we should see this on our screen.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/785/1*iHuwk7JGt4ZKiunyYSuy1Q.png" /><figcaption>React Start Screen</figcaption></figure><h3>Putting it all together</h3><p>Now, we should have two applications ready to go, the Rails back-end and the React front-end. In this step, we will link the two separate applications together, successfully allowing the front-end application, React, to store and retrieve data from the back-end, Rails.</p><p>In order to do this, we will need to use an interface for making API calls. We could use the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API">Fetch</a> API to make our API requests, but I tend to prefer <a href="https://github.com/axios/axios">Axios</a>. Either will work just fine.</p><p>First, we will need to install Axios in our client directory.</p><pre>$ npm install axios</pre><p>After the installation is complete, we are ready to start making API calls from our front-end to our back-end.</p><p>Essentially, we are going to take our Rails server on port 3001 and its name spaced API, and pass this information to Axios along with the relevant path.</p><p>For example, if we want to call a GET request for our hypothetical users page, our request would look something like this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/86ef0cdae4b89054c0c0f166db117d89/href">https://medium.com/media/86ef0cdae4b89054c0c0f166db117d89/href</a></iframe><p>In the example above, we are calling the Rails server https://localhost:3001, the name space we are using /api/v1, and finally, the path we want /users. Obviously, your own applications will differ.</p><p>In any case, we pass this information to the ‘get’ function, which makes the call to the Rails API, and process what is returned in the ‘then’ function, and finally, if there are any errors, we catch them in, you guessed it, the ‘catch’ function. Check out the <a href="https://github.com/axios/axios">Axios</a> docs for more information.</p><p>Also, notice the name spaced API comes into play here: <strong>/api/v1/</strong>some_path</p><p>Any change we make to that name space in our Rails-API, will also be reflected in our API requests. I know its probably obvious by now, but I thought I would mention it.</p><h3>Final thoughts</h3><p>There we go, we’ve successfully setup our Rails API, React front-end, and we have linked them together with Axios. This will get us up and running if we want a full-stack application but would like to separate our front-end from the back-end.</p><p>However, there are some quality-of-life changes that can be made to streamline the workflow within a system like this, but that’s a topic for another time.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4b4d3076545" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/setting-up-rails-6-with-react-4b4d3076545">Setting up Rails 6 with React</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>