<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Learnetto</title>
    <description>The latest articles on Forem by Learnetto (@learnetto).</description>
    <link>https://forem.com/learnetto</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%2Forganization%2Fprofile_image%2F1431%2F61208afc-e816-4a10-8fc8-6bd085a3a9cc.png</url>
      <title>Forem: Learnetto</title>
      <link>https://forem.com/learnetto</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/learnetto"/>
    <language>en</language>
    <item>
      <title>Ruby on Rails Quiz</title>
      <dc:creator>Hrishi Mittal</dc:creator>
      <pubDate>Wed, 31 Jul 2024 08:55:38 +0000</pubDate>
      <link>https://forem.com/learnetto/ruby-on-rails-quiz-29e3</link>
      <guid>https://forem.com/learnetto/ruby-on-rails-quiz-29e3</guid>
      <description>&lt;p&gt;I made a quiz for Rails developers - &lt;a href="https://learnetto.com/rails-quiz?utm_source=devto&amp;amp;utm_campaign=railsquiz" rel="noopener noreferrer"&gt;https://learnetto.com/rails-quiz&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How many questions can you answer correctly?&lt;/p&gt;

&lt;p&gt;Post your score in the comments!&lt;/p&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
      <category>learning</category>
      <category>hotwire</category>
    </item>
    <item>
      <title>Interview with Kevin Cunningham on software development, teaching and headless WordPress</title>
      <dc:creator>Hrishi Mittal</dc:creator>
      <pubDate>Tue, 20 Apr 2021 19:33:03 +0000</pubDate>
      <link>https://forem.com/learnetto/interview-with-kevin-cunningham-on-software-development-teaching-and-headless-wordpress-2j6j</link>
      <guid>https://forem.com/learnetto/interview-with-kevin-cunningham-on-software-development-teaching-and-headless-wordpress-2j6j</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d8UoQf62--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hmn7dfezm0mqgukbuj4a.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d8UoQf62--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hmn7dfezm0mqgukbuj4a.jpg" alt="Kevin Cunningham"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Today, I am interviewing&lt;/em&gt; &lt;a href="https://kevincunningham.co.uk/"&gt;&lt;em&gt;Kevin Cunningham&lt;/em&gt;&lt;/a&gt;&lt;em&gt;, a software developer, educator and author of the brand new&lt;/em&gt; &lt;a href="https://learnetto.com/users/dolearning/courses/headless-wordpress?utm_source=DEV&amp;amp;utm_medium=article&amp;amp;utm_campaign=interview_kevinc"&gt;&lt;em&gt;book on Headless WordPress&lt;/em&gt;&lt;/a&gt; &lt;em&gt;which recently launched on Learnetto.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This interview is part of the &lt;a href="https://learnetto.com?utm_source=DEV&amp;amp;utm_medium=article&amp;amp;utm_campaign=interview_kevinc"&gt;Learnetto&lt;/a&gt; Interview Series with interesting experts and teachers from all walks of life.&lt;/em&gt;  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Please introduce yourself - who are you and what do you do?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Hi! I’m Kevin Cunningham. I run a &lt;a href="https://spin-up.io/"&gt;digital prototyping agency called SpinUp&lt;/a&gt;, based in Brighton, England.   &lt;/p&gt;

&lt;p&gt;We work on helping businesses test their ideas by building high fidelity prototypes to test with their clients and investors.  &lt;/p&gt;

&lt;p&gt;Alongside that, I teach web development though egghead, blogging, Twitter, streams and, most recently, writing books.&lt;/p&gt;

&lt;p&gt;Away from the screen, I hang out with my wife and two boys. We try to get outside a lot but with lockdown that’s proved more and more challenging. We’re looking forward to getting back together with friends and getting out camping and adventuring soon.  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://learnetto.com/events/free-headless-wordpress-workshop?utm_source=DEV&amp;amp;utm_medium=article&amp;amp;utm_campaign=interview_kevinc"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PMZdG-JE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fzs3nlrsscp5mrnrkhwe.png" alt="Free Headless WordPress Workshop with Kevin Cunningham on Learnetto" title="Join Kevin for a Free Workshop on Headless WordPress on Friday 20th April"&gt;&lt;br&gt;
&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How did you get into software development?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I have been programming since I was a kid. I was your typical computer and Maths geek.  &lt;/p&gt;

&lt;p&gt;I studied Maths and Theoretical Physics at university having been discouraged to go into computing by my secondary Maths teacher.  &lt;/p&gt;

&lt;p&gt;After I finished my degree in Belfast, I moved to Brighton to become a Maths teacher myself. I taught 11-18 year olds for 13 years, working in a variety of roles and schools in and around Brighton. &lt;/p&gt;

&lt;p&gt;Programming had been going on in the background as I made little apps for my students and worked with code for fun.  &lt;/p&gt;

&lt;p&gt;About 4 years ago, I took the plunge and transitioned from being a Maths teacher to being a web developer.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What got you interested in teaching?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I’ve always been teaching. I was tutoring at uni and then was a full-time teacher. When I switched careers, I wasn’t sure if those skills would be relevant or useful.  &lt;/p&gt;

&lt;p&gt;After a few years, I was missing it a lot. So, I coached a bit at Codebar, became an egghead instructor, blogged and, now, wrote this book.&lt;/p&gt;

&lt;p&gt;What gets me excited about teaching is thinking about the impact that it can have on others. I can make mistakes that others don’t have to.  &lt;/p&gt;

&lt;p&gt;I can learn things and share them in ways that are useful to other people.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What differences have you noticed between teaching online and teaching in-person?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When teaching in person you can read a room or individual. You can do that somewhat with video calls, responding to quizzical glances or unsure expressions.  &lt;/p&gt;

&lt;p&gt;With pre-recorded video and text tutorials you are guessing more about what people don’t know. You can’t react to the person in front of you. So you find yourself posing and answering questions yourself.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tell us about your experience with WordPress.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;My first professional development was with a PHP Content Management System called Drupal. WordPress has been in the background for the past 20 years as I’ve helped to support my wife and friends. Setting up and debugging web-servers and getting themes up and running. &lt;/p&gt;

&lt;p&gt;More recently, I’ve worked with Sega to develop a WordPress/Gatsby solution for them.  &lt;/p&gt;

&lt;p&gt;It’s one of the options we offer to our clients as we think through the solutions that are going to work for them.  &lt;/p&gt;

&lt;p&gt;I enjoy dipping into the PHP and the JavaScript side of the system, building plugins and themes.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What do you like about WordPress?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I like that WordPress is well known. I like that it has such a well developed plugin ecosystem that means you can often find a solution for any problem.  &lt;/p&gt;

&lt;p&gt;I like how easy it is to get up and running, how most ISPs have a one-click WordPress install. I feel like it has a low barrier to entry to have a robust and reliable way to manage content.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What's Headless WordPress?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;WordPress is a big piece of software. It is a way to create your content, manage your posts, create images and schedule releases. Do all of those admin-y bits.  &lt;/p&gt;

&lt;p&gt;It also provides the mechanisms for people to be able to view that content. WordPress then provides the back-end and the front-end. &lt;/p&gt;

&lt;p&gt;Headless WordPress says what if we don’t use the native WordPress solution for the frontend?  &lt;/p&gt;

&lt;p&gt;So, we keep all the amazing functionality and capability of the back-end (creating, editing, curating) and we make that content available through an API. This could be either RESTful or using GraphQL.  &lt;/p&gt;

&lt;p&gt;Then, on the front-end, we can use any tool that can consume data from an API and render it. So, that could be a React framework, it could be Vue, it could be Svelte - anything that can take content and build it.  &lt;/p&gt;

&lt;p&gt;That way, you have more flexibility to change the frontend and experiment. The data stays consistent but the presentation changes.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why is it a good choice for a CMS?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are a lot of paid headless CMS solutions out there. Mostly, they are very good.  &lt;/p&gt;

&lt;p&gt;What is true with most software, is that it fills 80% of what you need but you need to bend it to be able to fill that last 20% of your use case. This is sometimes referred to as the Pareto principle. With those paid solutions, these adaptations are challenging.&lt;/p&gt;

&lt;p&gt;With WordPress, you’re in complete control of how your API works and how it responds to requests. It provides reliability and flexibility in almost unparalleled ways.  &lt;/p&gt;

&lt;p&gt;All while being free and open source. I love it!  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tell us about your new book&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So my new book, &lt;a href="https://learnetto.com/users/dolearning/courses/headless-wordpress?utm_source=DEV&amp;amp;utm_medium=article&amp;amp;utm_campaign=interview_kevinc"&gt;Off With Your Head!&lt;/a&gt;, is a guide to get up and running with headless WordPress.  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://learnetto.com/users/dolearning/courses/headless-wordpress?utm_source=DEV&amp;amp;utm_medium=article&amp;amp;utm_campaign=interview_kevinc"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Du8tCZJU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wuvmqobupil1wsgir32z.png" alt="Learn Headless WordPress" title="Join Kevin for a Free Workshop on Headless WordPress on Friday 20th April"&gt;&lt;br&gt;
&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In fact, just a vanilla install of WordPress makes a RESTful API available out of the box. But, in the book, I talk about how you can modify that API and make it present data the way you want. Add new endpoints and manipulate the data before it gets to the frontend.&lt;/p&gt;

&lt;p&gt;Also, I look at how you can set up WordPress as a GraphQL solution. How you can provide for all of your website needs from one consistent datastore.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where can people connect with you and follow all your work?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I’m on Twitter as &lt;a href="https://twitter.com/dolearning"&gt;@dolearning&lt;/a&gt;, I’m an instructor at egghead.io, I blog at &lt;a href="https://kevincunningham.co.uk"&gt;https://kevincunningham.co.uk&lt;/a&gt; and send out a weekly newsletter that you can signup for at &lt;a href="https://kevincunningham.co.uk/newsletter"&gt;https://kevincunningham.co.uk/newsletter&lt;/a&gt;.  &lt;/p&gt;

&lt;p&gt;Feel free to reach out and connect with me - I love that!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://learnetto.com/events/free-headless-wordpress-workshop?utm_source=DEV&amp;amp;utm_medium=article&amp;amp;utm_campaign=interview_kevinc"&gt;&lt;strong&gt;Join Kevin for a Free Workshop on Headless WordPress on Friday 20th April&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>wordpress</category>
      <category>headlesswordpress</category>
      <category>teaching</category>
      <category>education</category>
    </item>
    <item>
      <title>How to use React with Ruby on Rails 6</title>
      <dc:creator>Hrishi Mittal</dc:creator>
      <pubDate>Sun, 08 Dec 2019 14:47:17 +0000</pubDate>
      <link>https://forem.com/learnetto/how-to-use-react-with-ruby-on-rails-6-5n2</link>
      <guid>https://forem.com/learnetto/how-to-use-react-with-ruby-on-rails-6-5n2</guid>
      <description>&lt;p&gt;&lt;em&gt;Note: This tutorial is based on the latest versions of Rails (6.0.1) and React (16.12)&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;There are a few different ways to use React inside Ruby on Rails apps. With the recent improvements in native support in Rails for JavaScript tooling, it's now easier than ever to start using JavaScript libraries, including React, with Rails.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;webpacker&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;react-rails&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;react_on_rails&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;You can also use React in a separate frontend app with a Rails API. A new tutorial on that is coming shortly. &lt;a href="https://learnetto.com/newsletter" rel="noopener noreferrer"&gt;Sign up to the Learnetto newsletter&lt;/a&gt; to get it first.&lt;/strong&gt;&lt;/p&gt;

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

&lt;p&gt;This tutorial is based on Ruby 2.6.5 and the latest Rails gem (6.0.1 at the time of writing this tutorial).&lt;/p&gt;

&lt;p&gt;Create a new Rails app:&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 eventlite --webpack=react
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a Rails app with Webpack configured and a new directory for storing JavaScript code and a couple of new files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app/javascript:
  └── packs:
      └── application.js
      └── hello_react.jsx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All files in the new app/javascript/packs directory are compiled by Webpack.&lt;/p&gt;

&lt;p&gt;There's one example React file hello_react.jsx, which defines a simple functional React component called Hello that can display the words "Hello React!" on a page. It can also take a prop called name to display it instead of the word "React".&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 ReactDOM from 'react-dom'
import PropTypes from 'prop-types'

const Hello = props =&amp;gt; (
  &amp;lt;div&amp;gt;Hello {props.name}!&amp;lt;/div&amp;gt;
)

Hello.defaultProps = {
  name: 'David'
}

Hello.propTypes = {
  name: PropTypes.string
}

document.addEventListener('DOMContentLoaded', () =&amp;gt; {
  ReactDOM.render(
    &amp;lt;Hello name="React" /&amp;gt;,
    document.body.appendChild(document.createElement('div')),
  )
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can use this component on any page by linking it with the &lt;code&gt;javascript_pack_tag&lt;/code&gt; helper method.&lt;/p&gt;

&lt;p&gt;We're going to build a simple one-page application for creating and listing events.&lt;/p&gt;

&lt;p&gt;So let's start by creating a new events view file &lt;code&gt;app/views/events/index.html.erb&lt;/code&gt; and use the &lt;code&gt;javascript_pack_tag&lt;/code&gt; method inside it to display the default example Hello 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;%= javascript_pack_tag 'hello_react' %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's create the associated controller and index action for events:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;And make that our root path in &lt;code&gt;config/routes.rb&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;root 'events#index'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we can start the Rails server:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Go to &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt; to see the result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--x_vYRkXm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://learnetto-blog.s3.amazonaws.com/blog/2019-12-05/1575575364173-hello_react.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--x_vYRkXm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://learnetto-blog.s3.amazonaws.com/blog/2019-12-05/1575575364173-hello_react.png" width="800" height="311"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;I focus on using React with webpacker in the new version of my &lt;a href="https://learnetto.com/users/hrishio/courses/react-rails-course" rel="noopener noreferrer"&gt;Complete React on Rails Course&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. react-rails
&lt;/h2&gt;

&lt;p&gt;react-rails is the official React community gem for integrating React with Rails. The main benefit of using this gem is the react_component helper method which makes it easy to pass Rails data to components.&lt;/p&gt;

&lt;p&gt;You can use react-rails with webpacker or with Sprockets (to bundle the JSX into the asset pipeline). &lt;/p&gt;

&lt;p&gt;a. Using react-rails with webpacker&lt;/p&gt;

&lt;p&gt;Add the react-rails gem to the Gemfile of the app we created above and install it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gem 'react-rails'
&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;$ bundle install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then run the react-rails generator:&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 react:install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates the app/javascript/components/ directory for storing your React components. It also sets up configuration for mounting components and server-side rendering.&lt;/p&gt;

&lt;p&gt;The gem adds the following lines in &lt;code&gt;application.js&lt;/code&gt; to load the gem's JavaScript code (react_ujs) and all code inside the components directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var componentRequireContext = require.context("components", true);
var ReactRailsUJS = require("react_ujs");
ReactRailsUJS.useContext(componentRequireContext);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can use the same Hello component we created earlier with webpacker by moving it to the new components directory. We also need to rename the file to 'Hello.jsx' (case sensitive).&lt;/p&gt;

&lt;p&gt;react-rails provides a convenient helper method called &lt;code&gt;react_component&lt;/code&gt; to render React components inside views.&lt;/p&gt;

&lt;p&gt;First let's export the component and remove the call to ReactDOM.render on document load. The component code will now 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;import React from 'react'
import ReactDOM from 'react-dom'
import PropTypes from 'prop-types'

const Hello = props =&amp;gt; (
  &amp;lt;div&amp;gt;Hello {props.name}!&amp;lt;/div&amp;gt;
)

Hello.defaultProps = {
  name: 'David'
}

Hello.propTypes = {
  name: PropTypes.string
}

export default Hello
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And now inside our view file &lt;code&gt;events/index.html.erb&lt;/code&gt;, we can replace the javascript_pack_tag with a call to react_component:&lt;/p&gt;

&lt;p&gt;&amp;lt;%= react_component("Hello") %&amp;gt;&lt;/p&gt;

&lt;p&gt;We can also pass another value for the name prop easily:&lt;/p&gt;

&lt;p&gt;&amp;lt;%= react_component("Hello", { name: "react-rails" }) %&amp;gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--497HN73l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://learnetto-blog.s3.amazonaws.com/blog/2019-12-06/1575637799312-hello-react-rails.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--497HN73l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://learnetto-blog.s3.amazonaws.com/blog/2019-12-06/1575637799312-hello-react-rails.png" width="784" height="382"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The react-rails gem also provides a component generator to automatically create a basic component by passing it a component name and props (with their proptypes). It's similar to Rails generators for creating models and scaffolds:&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 react:component HelloWorld greeting:string
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;b. Using react-rails with sprockets&lt;/p&gt;

&lt;p&gt;react-rails also gives you the option to continue using the default Rails asset pipeline to serve your React code, instead of using webpacker.&lt;/p&gt;

&lt;p&gt;You can use this option by installing just the react-rails gem without the webpacker gem, and following the same steps as above.&lt;/p&gt;

&lt;p&gt;Note that the webpacker gem is included in Rails by default since version 6. So, for an existing Rails 6 app, you'll need to uninstall it and move all your JavaScript code to the classic &lt;code&gt;app/assets/javascripts&lt;/code&gt; directory. Or you can generate a new Rails app without webpacker by using the --skip-javascript flag:&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 eventlite --skip-javascript
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I strongly recommend you avoid this route. It's best to use the new Rails standard webpacker so that you can get all the benefits of the latest JavaScript tooling and you don't create legacy code that will get out of date very quickly.&lt;/p&gt;

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

&lt;p&gt;The other most popular third-party gem for integrating React into Rails is the &lt;a href="https://github.com/shakacode/react_on_rails" rel="noopener noreferrer"&gt;react_on_rails gem&lt;/a&gt; created by ShakaCode.&lt;/p&gt;

&lt;p&gt;Similar to the react-rails gem, now that Rails already provides an easy way to use React via webpacker, you have to see if the stated benefits of the react_on_rails gem apply to your situation.&lt;/p&gt;

&lt;p&gt;The main benefits include better support for server side rendering, easy passing of props to React components (with the react_component method just like react-rails), and Redux and React Router integration.&lt;/p&gt;

&lt;p&gt;This gem requires webpacker. To use it in a new Rails app with webpacker, let's install the gem first.&lt;/p&gt;

&lt;p&gt;Add it to to the Gemfile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gem 'react_on_rails', '11.1.4' # Use the exact gem version to match npm version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The 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;After installing the gem, we must first commit the code to git or the gem's install generator won't run. This is an easy step to miss, so make sure you do it before you proceed.&lt;/p&gt;

&lt;p&gt;Once you've committed the code to git, run the install generator and start the Rails server:&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 react_on_rails:install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This generates some example code and configuration and installs some dependencies.&lt;/p&gt;

&lt;p&gt;This gem places all React code under a new &lt;code&gt;app/javascript/bundles&lt;/code&gt; directory. This is just a convention, you can use other directories if you like.&lt;/p&gt;

&lt;p&gt;The gem install generated automatically generates an example component HelloWorld stored in &lt;code&gt;app/javascript/bundles/HelloWorld/components/HelloWorld.jsx&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It also generates a file called &lt;code&gt;hello-world-bundle.js&lt;/code&gt; inside the &lt;code&gt;app/javascript/packs&lt;/code&gt; directory. This is where the HelloWorld component code is imported and registered with react_on_rails so that it can be used in views.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import ReactOnRails from 'react-on-rails';

import HelloWorld from '../bundles/HelloWorld/components/HelloWorld';

// This is how react_on_rails can see the HelloWorld in the browser.
ReactOnRails.register({
  HelloWorld,
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;react_on_rails also provides a react_component helper method for rendering components inside views and pass props to them. You can see an example in the automatically generated view &lt;code&gt;app/views/hello_world/index.html.erb&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;h1&amp;gt;Hello World&amp;lt;/h1&amp;gt;
&amp;lt;%= react_component("HelloWorld", props: @hello_world_props, prerender: false) %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Start the Rails server and visit &lt;a href="http://localhost:3000/hello_world" rel="noopener noreferrer"&gt;http://localhost:3000/hello_world&lt;/a&gt; to see the component in action:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dr9HAf01--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://learnetto-blog.s3.amazonaws.com/blog/2019-12-06/1575668448451-react_on_rails.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dr9HAf01--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://learnetto-blog.s3.amazonaws.com/blog/2019-12-06/1575668448451-react_on_rails.gif" width="400" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That’s a quick overview of 3 ways to get started with using React inside your Rails app.&lt;/p&gt;

&lt;p&gt;I recommend starting with webpacker and using other gems only if you need their specific benefits.&lt;/p&gt;

&lt;p&gt;Once an app is big and complex enough, a lot of developer teams choose to move their React code into a separate frontend app and use the Rails app as a backend API.&lt;/p&gt;

&lt;p&gt;I’ll shortly publish another tutorial teaching the basics of that. &lt;a href="https://learnetto.com/newsletter" rel="noopener noreferrer"&gt;Sign up to the Learnetto newsletter&lt;/a&gt; to get it first.&lt;/p&gt;

&lt;p&gt;And if you want to learn it all in depth to use React professionally with Rails, &lt;strong&gt;check out the new version of my &lt;a href="https://learnetto.com/users/hrishio/courses/react-rails-course" rel="noopener noreferrer"&gt;Complete React on Rails Course&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Interview with Ajdin Imsirovic on Angular and frontend web development</title>
      <dc:creator>Hrishi Mittal</dc:creator>
      <pubDate>Sun, 01 Dec 2019 11:03:33 +0000</pubDate>
      <link>https://forem.com/learnetto/interview-with-ajdin-imsirovic-on-angular-and-frontend-web-development-23hb</link>
      <guid>https://forem.com/learnetto/interview-with-ajdin-imsirovic-on-angular-and-frontend-web-development-23hb</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UgB4iQ3x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://learnetto-blog.s3.amazonaws.com/blog/2019-11-30/1575131847307-ajdin.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UgB4iQ3x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://learnetto-blog.s3.amazonaws.com/blog/2019-11-30/1575131847307-ajdin.jpg" width="200" height="200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Today I'm pleased to interview Ajdin Imsirovic, senior developer, author and teacher of two brand new Angular courses on Learnetto - &lt;a href="https://learnetto.com/users/ajdika/courses/the-free-visual-guide-to-getting-started-with-angular" rel="noopener noreferrer"&gt;The Free Visual Guide to Getting Started with Angular&lt;/a&gt; and &lt;a href="https://learnetto.com/users/ajdika/courses/the-complete-practical-angular-course" rel="noopener noreferrer"&gt;The Complete Practical Angular Course&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;In this interview, Ajdin shares his thoughts on Angular and frontend web development.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Please introduce yourself.&lt;/strong&gt;  &lt;/p&gt;

&lt;p&gt;My name is Ajdin Imsirovic. I'm a self-taught full stack web developer. I started off as a print designer, then slowly moved into digital media (web design), and finally, bit by bit, into web development. Initially, I only did HTML and CSS, then SCSS, then JavaScript, and now I'm working in several front-end languages and libraries, and with PHP and Ruby on the backend.&lt;/p&gt;

&lt;p&gt;I blog actively on &lt;a href="https://www.codingexercises.com/" rel="noopener noreferrer"&gt;https://www.codingexercises.com&lt;/a&gt; and have written several &lt;a href="https://amazon.com/author/ajdinimsirovic" rel="noopener noreferrer"&gt;technical books&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What got you into programming?&lt;/strong&gt;  &lt;/p&gt;

&lt;p&gt;I built my first web page as a table-based layout in 2001. The web was young back then, and it was a fun, novel technology to play around with (I still feel the same way today!). However, I didn't really get into full time coding till maybe 10 years later.   &lt;/p&gt;

&lt;p&gt;In the meantime, I was mostly designing on the side and working in non tech-related jobs. Regardless, all that time I kept following the progress of web technologies, and every once in a while, I'd dabble a bit with mostly front-end development.  &lt;/p&gt;

&lt;p&gt;Finally, in 2011 I got hired as a webmaster, then as a web developer, and it went on from there.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why do you like Angular?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's say there are several obstacles you're trying to solve as a web developer:  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Stay relevant and improve your skills.&lt;/li&gt;
&lt;li&gt; Use a complete framework that helps you be productive.&lt;/li&gt;
&lt;li&gt; Balance challenging tasks with familiar tasks so that you can grow and your job is not boring.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I believe Angular is a right mix of several competing solutions, each one pulling in their own direction.  &lt;/p&gt;

&lt;p&gt;For example, you need to stay relevant, by learning new things. So, if you know JavaScript, but don't know TypeScript, Angular will help you there since it's the default language to use.  &lt;/p&gt;

&lt;p&gt;Additionally, it's a complete framework: it comes with batteries included. For example, you don't have to think about the best way to perform XHR requests, it's built-in; you just need to know how to use the features as intended.  &lt;/p&gt;

&lt;p&gt;Once you do know it, each project will have a level of similarity that makes you become more productive, faster.  &lt;/p&gt;

&lt;p&gt;Angular also has enough different features that there's always something new to explore and get better at - thus you're never bored. But you are still staying inside the Angular ecosystem, which is helping you master your craft.  &lt;/p&gt;

&lt;p&gt;Long story short, I only have words of praise for it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How does it compare to other frontend libraries and frameworks?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I've already sort of answered the question, but if you'd like a more direct comparison, here's my take on it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; I don't like React since it's too flexible. It's like somebody gave you all the parts of a car, feel free to assemble it yourself. However, being curious as I am, I'm still watching React and making mini projects in it here and there, but Angular just feels like a better fit for me. &lt;/li&gt;
&lt;li&gt; It reminds me of Elm because very often it won't compile if you messed something up. For example, if you accidentally mess up your HTML templates, it will throw an error saying: Template parse errors. So it safeguards you against your own mistakes - to a point. One complaint could be that Elm catches many more errors then Angular does, because Angular's compiler is not as strict, but it's strict enough for my taste.&lt;/li&gt;
&lt;li&gt; Vue is sort of like an offshoot of Angular. It's a strong contender in my opinion; I like it, but it doesn't feel as enterprise-ready as Angular, and the market share is not as big.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;However, all of these answers need to be taken with a grain of salt. Lots of online comparisons of frameworks really boil down to personal preference and to what is actually used at your place of work.  &lt;/p&gt;

&lt;p&gt;The best advice, in my opinion, is to try them all, then decide which one works best for you.  &lt;/p&gt;

&lt;p&gt;This is a significant effort, but it's still doable. Even if you decide to try something like that, I'd say you should try Angular right after you do it in vanilla JavaScript and/or jQuery.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What's your favourite thing about Angular?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The standardized way of doing things is my number 1 Angular feature.  &lt;/p&gt;

&lt;p&gt;People say React has got more jobs and more projects online, but Angular is better when these projects become bigger. So it is sort of expected that bigger projects should/will use Angular on the frontend. It's a stretch, but this might mean that you'll have better job stability since you'd be working for enterprise clients or directly in enterprise firms.  &lt;/p&gt;

&lt;p&gt;Also, standardization helps avoid things getting messy as your projects grow bigger. Additionally, standardization makes it easier to add new features, because most of the times, there are only a few suggested/expected ways of doing things in Angular.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What kind of projects is Angular best for? What projects have you used it for?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Angular is best-suited for large projects with lots of features. I've used it to build some very complex web apps that included lots of logic on the front-end.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How do you recommend people start learning Angular?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Since I'm a teacher by vocation and a self-taught web developer in practice, I'm really curious - even obsessive - about the learning process. Like any web-related technology, it is best to learn it by doing. However, it's important to not lose too much time learning the basics, because it takes time to learn Angular.  &lt;/p&gt;

&lt;p&gt;My suggestion is start with &lt;a href="https://learnetto.com/users/ajdika/courses/the-free-visual-guide-to-getting-started-with-angular" rel="noopener noreferrer"&gt;The Free Visual Guide to Getting Started with Angular&lt;/a&gt; and if you like it, enrol in &lt;a href="https://learnetto.com/users/ajdika/courses/the-complete-practical-angular-course" rel="noopener noreferrer"&gt;The Complete Practical Angular Course&lt;/a&gt; to go in-depth while building a complete Angular application.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>11 great quotes from The Tao of Programming</title>
      <dc:creator>Hrishi Mittal</dc:creator>
      <pubDate>Mon, 11 Sep 2017 22:55:06 +0000</pubDate>
      <link>https://forem.com/learnetto/11-great-quotes-from-the-tao-of-programming</link>
      <guid>https://forem.com/learnetto/11-great-quotes-from-the-tao-of-programming</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://learnetto.com/blog/11-great-quotes-from-the-tao-of-programming" rel="noopener noreferrer"&gt;Learnetto.com&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The Tao of Programming by Geoffrey James is a strange and funny book. It’s one of my all-time favourites.&lt;/p&gt;

&lt;p&gt;These are some of my favourite quotes from the book:&lt;/p&gt;

&lt;p&gt;“&lt;em&gt;After three days without programming, life becomes meaningless.&lt;/em&gt;”&lt;/p&gt;

&lt;p&gt;“&lt;em&gt;I don’t know whether I am Turing dreaming that I am a machine, or a machine dreaming that I am Turing!&lt;/em&gt;”&lt;/p&gt;

&lt;p&gt;“&lt;em&gt;When a program is being tested, it is too late to make design changes.&lt;/em&gt;”&lt;/p&gt;

&lt;p&gt;“&lt;em&gt;A well-written program is its own Heaven; a poorly-written program is its own Hell.&lt;/em&gt;”&lt;/p&gt;

&lt;p&gt;“&lt;em&gt;But how will I know when I have received Enlightenment?” asked the novice. “Your program will run correctly,” replied the Master.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;“&lt;em&gt;Though a program be but three lines long, someday it will have to be maintained.&lt;/em&gt;”&lt;/p&gt;

&lt;p&gt;“&lt;em&gt;Let the programmers be many and the managers fewâ€Š–â€Šthen all will be productive.&lt;/em&gt;”&lt;/p&gt;

&lt;p&gt;“&lt;em&gt;You can demonstrate a program for a corporate executive, but you can’t make him computer literate.&lt;/em&gt;”&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The Master Programmer moves from program to program without fear. No change in management can harm him. He will not be fired, even if the project is cancelled. Why is this? He is filled with Tao.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;“&lt;em&gt;Without the wind, the grass does not move. Without software hardware is useless.&lt;/em&gt;”&lt;/p&gt;

&lt;p&gt;You can &lt;a href="https://www.amazon.com/Tao-Programming-Geoffrey-James/dp/0931137071/" rel="noopener noreferrer"&gt;get a copy on Amazon&lt;/a&gt; or &lt;a href="https://paranormal.se/article/tao-of-programming.pdf" rel="noopener noreferrer"&gt;read it online here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>devtips</category>
      <category>life</category>
      <category>book</category>
      <category>zen</category>
    </item>
    <item>
      <title>The 11-minute guide to building and launching an online store with Rails, Stripe Checkout and Heroku</title>
      <dc:creator>Hrishi Mittal</dc:creator>
      <pubDate>Sun, 10 Sep 2017 17:06:53 +0000</pubDate>
      <link>https://forem.com/learnetto/the-11-minute-guide-to-building-and-launching-an-online-store-with-rails-stripe-checkout-and-heroku</link>
      <guid>https://forem.com/learnetto/the-11-minute-guide-to-building-and-launching-an-online-store-with-rails-stripe-checkout-and-heroku</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://learnetto.com/tutorials/the-11-minute-guide-to-building-and-launching-an-online-store-with-rails-stripe-checkout-and-heroku" rel="noopener noreferrer"&gt;Learnetto.com&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Learn to build and launch &lt;a href="http://stickers.win" rel="noopener noreferrer"&gt;a real online store&lt;/a&gt; in this 11-minute video tutorial. We will build a simple store which accepts payments through Stripe checkout.js and deploy it to Heroku.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/BjXsB88fTtQ"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>rails</category>
      <category>stripe</category>
      <category>heroku</category>
      <category>ruby</category>
    </item>
    <item>
      <title>How to include Rails Associated Model Data in React components</title>
      <dc:creator>Hrishi Mittal</dc:creator>
      <pubDate>Wed, 06 Sep 2017 11:41:31 +0000</pubDate>
      <link>https://forem.com/learnetto/how-to-include-rails-associated-model-data-in-react-components</link>
      <guid>https://forem.com/learnetto/how-to-include-rails-associated-model-data-in-react-components</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.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%2F1mk5tn9848m0yismgu41.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F1mk5tn9848m0yismgu41.png" width="800" height="447"&gt;&lt;/a&gt;  &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://learnetto.com/blog/how-to-include-rails-associated-model-data-in-react-components" rel="noopener noreferrer"&gt;Learnetto.com&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;When you start using React with Rails, one of the frustrating problems you quickly run into is trying to access associated model data from your React component.  &lt;/p&gt;

&lt;p&gt;Let's say we areÂ &lt;a href="https://learnetto.com/tutorials/how-to-build-a-chat-app-with-rails-5-1-actioncable-and-react-js-part-1" rel="noopener noreferrer"&gt;&lt;strong&gt;building a chat app with Rails and React&lt;/strong&gt;&lt;/a&gt;. We have three models, Chatroom, Message and User, which have the following relationships between them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Chatroom&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;has_many&lt;/span&gt; &lt;span class="ss"&gt;:messages&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;dependent: :destroy&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Message&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;belongs_to&lt;/span&gt; &lt;span class="ss"&gt;:chatroom&lt;/span&gt;
  &lt;span class="n"&gt;belongs_to&lt;/span&gt; &lt;span class="ss"&gt;:user&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;has_many&lt;/span&gt; &lt;span class="ss"&gt;:messages&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;dependent: :destroy&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And we have aÂ &lt;a href="https://github.com/learnetto/reactchat/blob/master/client/app/bundles/Chatroom/components/Message.jsx" rel="noopener noreferrer"&gt;Message component&lt;/a&gt;Â for rendering individual messages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; 

    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Message&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In addition to the message body we also want to display the user's name.  &lt;/p&gt;

&lt;p&gt;If you're using a standard Rails ERB or Haml view template, you can simply write something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sx"&gt;%= @message.user.name %&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that will just work even if you didn't include the user data in your database query in the controller.  &lt;/p&gt;

&lt;p&gt;However,Â  unlike in a Rails view, if we callÂ &lt;code&gt;message.user.name&lt;/code&gt;Â inside our React component without having specifically included that data in the prop sent to the component, it will throw an error.Â   &lt;/p&gt;

&lt;p&gt;While a Rails template is actually able to call the model on-the-fly and get data it doesn't have, we don't have that luxury with React.  &lt;/p&gt;

&lt;p&gt;We need to explicitly compose the JSON with the associated User model data because a React component can only access the JSON data we provide it.  &lt;/p&gt;

&lt;p&gt;There are many ways to include the associated model data, including manually composing the JSON in the controller, defining a customÂ &lt;em&gt;as_json&lt;/em&gt;Â methodÂ  on the model or usingÂ &lt;a href="https://github.com/rails-api/active_model_serializers" rel="noopener noreferrer"&gt;ActiveModelSerializers&lt;/a&gt;.  &lt;/p&gt;

&lt;p&gt;One of the cleanest and most flexible ways is to useÂ &lt;a href="https://github.com/rails/jbuilder" rel="noopener noreferrer"&gt;jbuilder&lt;/a&gt;, which gives you a simple Domain Specific Language (DSL) for declaring JSON structures. The jbuilder gem comes included with Rails.  &lt;/p&gt;

&lt;p&gt;In the example above, we can include our user data in the message JSON by defining it in aÂ &lt;a href="https://github.com/learnetto/reactchat/blob/master/app/views/messages/_message.json.jbuilder" rel="noopener noreferrer"&gt;_message.json.jbuilder&lt;/a&gt;Â  file like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;user&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;extract!&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:image&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The jbuilder DSL is quite powerful and allows you to do all sorts of custom structuring of the data.  &lt;/p&gt;

&lt;p&gt;For example, let's say we want to send all the messages in chronological order and user data for a particular chatroom to a Chatroom component. We can define theÂ &lt;a href="https://github.com/learnetto/reactchat/blob/master/app/views/chatrooms/_chatroom.json.jbuilder" rel="noopener noreferrer"&gt;chatroom json&lt;/a&gt;Â like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chatroom&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chatroom&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;messages&lt;/span&gt;
                &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort_by&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:created_at&lt;/span&gt;&lt;span class="p"&gt;]})&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;extract!&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:body&lt;/span&gt;
  &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;user&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;extract!&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:image&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Check out theÂ &lt;a href="https://github.com/rails/jbuilder" rel="noopener noreferrer"&gt;jbuilder documentation&lt;/a&gt;Â to learn more about all its features.  &lt;/p&gt;

&lt;p&gt;The full code of the Chat app isÂ &lt;a href="https://github.com/learnetto/reactchat" rel="noopener noreferrer"&gt;on Github&lt;/a&gt;Â and there's a &lt;a href="https://reactrooms.herokuapp.com" rel="noopener noreferrer"&gt;live demo running here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can also learn how to build it yourself in this two-part video tutorial - Â &lt;a href="https://learnetto.com/tutorials/how-to-build-a-chat-app-with-rails-5-1-actioncable-and-react-js-part-1" rel="noopener noreferrer"&gt;How to build a chat app with Rails 5.1 ActionCable and React.js Part 1&lt;/a&gt;Â andÂ &lt;a href="https://learnetto.com/tutorials/how-to-build-a-chat-app-with-rails-5-1-actioncable-and-react-js-part-2" rel="noopener noreferrer"&gt;Part 2&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>react</category>
      <category>rails</category>
      <category>javascript</category>
      <category>ruby</category>
    </item>
    <item>
      <title>How to make AJAX calls in Rails 5.1</title>
      <dc:creator>Hrishi Mittal</dc:creator>
      <pubDate>Wed, 23 Aug 2017 11:10:18 +0000</pubDate>
      <link>https://forem.com/learnetto/how-to-make-ajax-calls-in-rails-51</link>
      <guid>https://forem.com/learnetto/how-to-make-ajax-calls-in-rails-51</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.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%2F5n8odoo1k48ssrvv1qwr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F5n8odoo1k48ssrvv1qwr.png" width="800" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://learnetto.com/blog/how-to-make-ajax-calls-in-rails-5-1" rel="noopener noreferrer"&gt;Learnetto.com&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Rails shipped without jQuery as a dependency for the first time with version 5.1. Rails used to rely on jQuery forÂ &lt;a href="http://guides.rubyonrails.org/working_with_javascript_in_rails.html#unobtrusive-javascript" rel="noopener noreferrer"&gt;&lt;strong&gt;Unobtrusive JavaScript&lt;/strong&gt;&lt;/a&gt;Â features but now ships with its own libraryÂ &lt;a href="https://github.com/rails/rails/tree/master/actionview/app/assets/javascripts" rel="noopener noreferrer"&gt;&lt;strong&gt;rails-ujs&lt;/strong&gt;&lt;/a&gt;Â with the same features.  &lt;/p&gt;

&lt;p&gt;One of the most common use cases for jQuery in Rails was making AJAX calls through forms and links. If you’re using Rails 5.1 for the first time, you might get confused about how to make AJAX calls, especially things like data formats and including CSRF tokens.  &lt;/p&gt;

&lt;p&gt;So this little tutorial walks you through a few ways you can do so:&lt;br&gt;&lt;br&gt;
&lt;strong&gt;1. Use rails-ujs (no jQuery)&lt;/strong&gt;  &lt;/p&gt;

&lt;p&gt;A new Rails 5.1 app automatically includes the rails-ujs script in the app’s application.js file:&lt;br&gt;
&lt;/p&gt;

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

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

&lt;/div&gt;



&lt;p&gt;If you don’t want to include it there and use it only selectively, say if you’re making a separate React frontend with Rails’s new webpacker support, then you can do use rails-ujs by installing it as a package:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ yarn add rails-ujs

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

&lt;/div&gt;



&lt;p&gt;And then import and start it in your JavaScript file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Rails&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="nx"&gt;â&lt;/span&gt;&lt;span class="err"&gt;€˜&lt;/span&gt;&lt;span class="nx"&gt;rails&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;ujs&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;
&lt;span class="nx"&gt;Rails&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Remove this line from application.js :&lt;br&gt;
&lt;/p&gt;

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

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

&lt;/div&gt;



&lt;p&gt;Now let’s say we want to make an AJAX POST call to an API endpoint/things with some data which looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;mydata&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="na"&gt;thing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;field1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;value1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;field2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;value2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Making an AJAX POST call with rails-ujs looks identical to making it with jQuery:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Rails&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ajax&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/things&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;mydata&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;success&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;repsonse&lt;/span&gt;&lt;span class="p"&gt;){...},&lt;/span&gt;
  &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;repsonse&lt;/span&gt;&lt;span class="p"&gt;){...}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Except for one thing! As far as I can tell, you can’t simply send JSON data. So we need to convert mydata to application/x-www-form-urlencoded content type manually like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;mydata&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;thing[field1]=value1&amp;amp;thing[field2]=value2&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;jQuery does this conversion automatically before submitting a request.  &lt;/p&gt;

&lt;p&gt;I assumed rails-ujs has some automatic way of doing it, but I couldn’t find anything in the docs orÂ &lt;a href="https://github.com/rails/rails/blob/master/actionview/app/assets/javascripts/rails-ujs/utils/ajax.coffee" rel="noopener noreferrer"&gt;&lt;strong&gt;code&lt;/strong&gt;&lt;/a&gt;.  &lt;/p&gt;

&lt;p&gt;If you know,Â &lt;a href="https://stackoverflow.com/questions/45820587/how-do-you-send-json-data-in-a-rails-ujs-rails-ajax-post-call-not-using-jquery" rel="noopener noreferrer"&gt;&lt;strong&gt;let me know&lt;/strong&gt;&lt;/a&gt;.  &lt;/p&gt;

&lt;p&gt;rails-ujs automatically takes care of including the CSRF token with the request.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Use jQuery&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If the manual data conversion puts you off, you can still use jQuery. You may be relying on jQuery for other things in your app anyway, so here are couple of ways of using jQuery with Rails for making AJAX calls:  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.1 Use the jquery-rails gem&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This gem used to be automatically included in previous versions of Rails. You can install it by adding it to your Gemfile:&lt;br&gt;
&lt;/p&gt;

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

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

&lt;/div&gt;



&lt;p&gt;Then 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;And include jquery and jquery_ujs in your application.js file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//= require jquery
//= require jquery_ujs

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

&lt;/div&gt;



&lt;p&gt;That’s all you need. Now you can make your AJAX call in the usual way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ajax&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/things&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;mydata&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;success&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;textStatus&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;jqXHR&lt;/span&gt;&lt;span class="p"&gt;){...},&lt;/span&gt;
  &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;jqXHR&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;textStatus&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;errorThrown&lt;/span&gt;&lt;span class="p"&gt;){...}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;jquery_ujs takes care of including the CSRF token and jquery converts the data to application/x-www-form-urlencoded content type automatically.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.2. Use jquery package from npm&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you don’t want to use a gemified version of jQuery, you can install it as a JavaScript package from npm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ yarn add jquery
$ yarn add jquery-ujs

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

&lt;/div&gt;



&lt;p&gt;Then import them in your JavaScript file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="nx"&gt;â&lt;/span&gt;&lt;span class="err"&gt;€˜&lt;/span&gt;&lt;span class="nx"&gt;jquery&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="nx"&gt;â&lt;/span&gt;&lt;span class="err"&gt;€˜&lt;/span&gt;&lt;span class="nx"&gt;jquery&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;ujs&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Make sure to remove the jquery-rails gem if you have it installed and remove jquery and jquery_ujs from application.js.Â   &lt;/p&gt;

&lt;p&gt;You may get an error saying jQuery is not defined. In your webpack config file add this to the config.plugins array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;webpack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ProvidePlugin&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
 &lt;span class="na"&gt;$&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;jquery&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="na"&gt;jQuery&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;“&lt;/span&gt;&lt;span class="nx"&gt;jquery&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;
&lt;span class="p"&gt;}),&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And now you will be able to use jQuery for making your AJAX calls.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Use axios&lt;/strong&gt;  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/mzabriskie/axios" rel="noopener noreferrer"&gt;&lt;strong&gt;axios&lt;/strong&gt;&lt;/a&gt;Â is a promise-based HTTP library. You can use it to make HTTP requests from node.js (on the server) and also AJAX requests from the browser.  &lt;/p&gt;

&lt;p&gt;It’s a good alternative if you don’t need any of jQuery’s other features. To use axios in a Rails 5.1 app (with webpacker), install it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ yarn add axios

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

&lt;/div&gt;



&lt;p&gt;Then import it in your JavaScript file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;axios&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="nx"&gt;â&lt;/span&gt;&lt;span class="err"&gt;€˜&lt;/span&gt;&lt;span class="nx"&gt;axios&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Now we can make the AJAX call like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;axios&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/things&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;mydata&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;X-CSRF-Token&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;meta[name=csrf-token]&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{...},&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{...}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A few things to note here:  &lt;/p&gt;

&lt;p&gt;We have to include the CSRF token header. Unlike jquery_ujs and rails-ujs, it’s not automatically included with axios requests.  &lt;/p&gt;

&lt;p&gt;We can specify it as a default setting to avoid repeating it in all our AJAX calls:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;csrfToken&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;meta[name=csrf-token]&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt; 

&lt;span class="nx"&gt;axios&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;defaults&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;common&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;â&lt;/span&gt;&lt;span class="err"&gt;€˜&lt;/span&gt;&lt;span class="nx"&gt;X&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;CSRF&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;Token&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;csrfToken&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;We can access the success and error responses using the promises syntaxÂ &lt;em&gt;.then&lt;/em&gt;.  &lt;/p&gt;

&lt;p&gt;Another option is to useÂ &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch" rel="noopener noreferrer"&gt;&lt;strong&gt;fetch&lt;/strong&gt;&lt;/a&gt;, but it’s an experimental API and not available by default in all browsers. So you’re best off usingÂ &lt;a href="http://fetch" rel="noopener noreferrer"&gt;&lt;strong&gt;Github’s polyfill library&lt;/strong&gt;&lt;/a&gt;. I prefer axios to fetch because fetch responses need to be first converted to json and the error handling can also be confusing.&lt;/p&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How to use React.js with Ruby on Rails 5</title>
      <dc:creator>Hrishi Mittal</dc:creator>
      <pubDate>Mon, 24 Jul 2017 09:08:54 +0000</pubDate>
      <link>https://forem.com/learnetto/how-to-use-reactjs-with-ruby-on-rails-5</link>
      <guid>https://forem.com/learnetto/how-to-use-reactjs-with-ruby-on-rails-5</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.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%2F62uey1lyi8tsbh3dk1pu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F62uey1lyi8tsbh3dk1pu.png" alt="React with Ruby on Rails" width="800" height="449"&gt;&lt;/a&gt;  &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://learnetto.com/blog/3-ways-to-use-react-with-ruby-on-rails-5" rel="noopener noreferrer"&gt;Learnetto.com&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IMPORTANT UPDATE (6 July 2017)&lt;/strong&gt;:Â  With the release of Rails 5.1, Rails now has native support for using React via the webpacker gem. Check out the free lesson on how to do this in theÂ &lt;a href="https://learnetto.com/users/hrishio/courses/the-free-react-on-rails-5-course" rel="noopener noreferrer"&gt;Free React on Rails course&lt;/a&gt;Â or seeÂ &lt;a href="https://medium.com/react-on-rails/free-tutorial-how-to-use-react-with-webpacker-and-rails-5-1-92af8e8d9d63" rel="noopener noreferrer"&gt;this blog post&lt;/a&gt;.  &lt;/p&gt;

&lt;p&gt;There are a few different ways to useÂ &lt;a href="https://facebook.github.io/react/" rel="noopener noreferrer"&gt;React&lt;/a&gt;Â inside Ruby on Rails apps. In this post, I'll cover 3 of the most popular ways.  &lt;/p&gt;

&lt;p&gt;*&lt;em&gt;1.Â *&lt;/em&gt;&lt;a href="https://github.com/reactjs/react-rails" rel="noopener noreferrer"&gt;react-rails gem (https://github.com/reactjs/react-rails)&lt;/a&gt;  &lt;/p&gt;

&lt;p&gt;The react-rails gem is the simplest and quickest way to start using React inside your Rails app.  &lt;/p&gt;

&lt;p&gt;It uses the default Rails asset management and automatically transforms JSX into the asset pipeline using the Ruby Babel transpiler.  &lt;/p&gt;

&lt;p&gt;Assuming you already have a recent version of Ruby and the Rails gem installed, here's how you get started with react-rails.  &lt;/p&gt;

&lt;p&gt;Add the gem to your Gemfile:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Install the gem:&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;Run the react-rails installation script:&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:install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a components directory, manifest file and add them to the application.js file.&lt;/p&gt;

&lt;p&gt;Then create your React component file with a .jsx extension and save it underÂ &lt;em&gt;app/assets/javascripts/components&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var HelloMessage = React.createClass({
  render: function() {
    return (
      &amp;lt;h1&amp;gt;Hello {this.props.name}!&amp;lt;/h1&amp;gt;
    )
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, use the component in your view with theÂ &lt;em&gt;react_component&lt;/em&gt;Â helper method.&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('HelloMessage', name: 'John') %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The method takes the name of the component as the first argument and any props as the second argument. It adds a div with the relevant class and props, which is then used by the react_ujs driver to mount and render the component.  &lt;/p&gt;

&lt;p&gt;That's all you need to get started!Â   &lt;/p&gt;

&lt;p&gt;There's a whole lot more react-rails offers, including choosing react builds for your environment, server-side rendering, writing components in ES6 and component generators, which are covered in ourÂ &lt;a href="https://learnetto.com/users/hrishio/courses/the-react-on-rails-course" rel="noopener noreferrer"&gt;React on Rails course&lt;/a&gt;.  &lt;/p&gt;

&lt;p&gt;This gem allows you to start with React gradually and test the waters by converting individual views into React components. Once you're happy and confident with it, you can convert more and/or move to a more powerful setup.  &lt;/p&gt;

&lt;p&gt;*&lt;em&gt;2.Â *&lt;/em&gt;&lt;a href="https://github.com/shakacode/react_on_rails" rel="noopener noreferrer"&gt;react_on_rails gem (https://github.com/shakacode/react_on_rails)&lt;/a&gt;  &lt;/p&gt;

&lt;p&gt;This is another very popular gem for integrating React in Rails. The key difference with react-rails is that it uses EcmaScript 6 (ES6) by default and state-of-the-art JavaScript tooling, including Webpack, instead of relying completely on the Rails asset pipepline. It alsoÂ  doesn't depend on jQuery.  &lt;/p&gt;

&lt;p&gt;Instead of using the Rails asset pipeline for ES6 compilation, react_on_rails only uses it for including the JS that Webpack compiles.  &lt;/p&gt;

&lt;p&gt;You can use npm to install JavaScript libraries, instead of having to use gems or manually downloading and including them.  &lt;/p&gt;

&lt;p&gt;It gives you a lot more power at the cost of installing and managing a few extra things.  &lt;/p&gt;

&lt;p&gt;To get started, you need to install node so that you can use npm to install JavaScript dependencies. You can download node directly fromÂ &lt;a href="https://nodejs.org/en/" rel="noopener noreferrer"&gt;their website&lt;/a&gt;Â or install it usingÂ &lt;a href="https://github.com/creationix/nvm" rel="noopener noreferrer"&gt;nvm&lt;/a&gt;.  &lt;/p&gt;

&lt;p&gt;Once node is installed, we can start by adding the gem to the Gemfile of our Rails app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gem "react_on_rails", "~&amp;gt; 6"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install it by running bundle:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Now, we need to commit this to git, otherwise the gem's installation script won't work.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git init
git add -A
git commit -m "Initial commit"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then run the gem's generator to create the package.json and Procfile.dev files:&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 react_on_rails:install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then run bundle again for installing execjs and npm install for installing JS dependencies:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Then, start the Rails server with foreman:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;foreman start -f Procfile.dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We're using foreman to run Webpack, in addition to the Rails server.&lt;/p&gt;

&lt;p&gt;Now that everything is set up, we can create and use React components.Â   &lt;/p&gt;

&lt;p&gt;The gem's generator also creates a client directory, which is where all client-side code needs to go.Â   &lt;/p&gt;

&lt;p&gt;A Hello World example is included under the client directory. Below is a simplified extract of the code for a component displaying a Hello message with a name passed as a prop.&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';

export default class HelloWorld extends React.Component {
  constructor(props, _railsContext) {
    super(props);
  }

  render() {
    return (
      &amp;lt;h1&amp;gt;Hello, {this.props.name}!&amp;lt;/h1&amp;gt;
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can use the react_component helper method to render the component in a view in the same way we did with the react-rails gem.&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("HelloWorldApp", props: {name: 'John'}) %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you're already familiar with Webpack and modern JavaScript concepts and tooling, then go right ahead and start with react_on_rails. Otherwise, it might be better to get a feel for the core concepts of React with react-rails first and then move on to react_on_rails.  &lt;/p&gt;

&lt;p&gt;Have a look at theÂ &lt;a href="https://blog.shakacode.com/the-react-on-rails-doctrine-3c59a778c724" rel="noopener noreferrer"&gt;React on Rails doctrine&lt;/a&gt;Â to get some insight into the design decisions behind react_on_rails.  &lt;/p&gt;

&lt;p&gt;**3. Rails API with a separate frontend React app (Nodejs + Express)  &lt;/p&gt;

&lt;p&gt;**A third option for using React with Rails is to separate the backend and frontend into two different apps.Â   &lt;/p&gt;

&lt;p&gt;The backend can either be a standard Rails app exposing some API endpoints with JSON output or it can be a Rails API-only app. The frontend is an Express app running on Nodejs, which uses React and talks to the Rails API.  &lt;/p&gt;

&lt;p&gt;Obviously, building, running and maintaining two separate apps is much more work. But there are some benefits.  &lt;/p&gt;

&lt;p&gt;Making an API may be a good idea anyway to enable you to build other clients like mobile apps (although note that you can use a Rails API with the gems too).  &lt;/p&gt;

&lt;p&gt;A Node app is the natural environment for a JavaScript library like React, so you may find you're more productive and always have access to the latest JS tooling by default. Server-side rendering of components is also faster.  &lt;/p&gt;

&lt;p&gt;If you work in a medium-large company with dedicated teams for backend and frontend development, then separating the two code bases makes sense.  &lt;/p&gt;

&lt;p&gt;You do have to deal with the complexities of running two apps. You either have to implement Cross Origin Resource Sharing (CORS) or proxy requests through a server like nginx.  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/@alexfedoseev/isomorphic-react-with-rails-part-i-440754e82a59" rel="noopener noreferrer"&gt;This article&lt;/a&gt;Â by Alex Fedoseev goes into some detail on what such a setup would look like. It's an excellent read to get an insight into what's involved and whether it's a suitable approach for you.  &lt;/p&gt;

&lt;p&gt;So that's 4 of the most popular methods of using React with Rails. Research and experiment with the ones you think make most sense for your situation and please leave a comment to share your experiences!  &lt;/p&gt;

&lt;p&gt;If you found this article useful, check outÂ &lt;a href="https://learnetto.com/users/hrishio/courses/the-react-on-rails-course" rel="noopener noreferrer"&gt;The Complete React on Rails course&lt;/a&gt;for detailed hands-on video lessons on using React with Rails.&lt;/p&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
      <category>react</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
