How Rails Developers Can Use React_785

React.js is one of the simplest JavaScript Frameworks that you can choose for your application development. This is one of the reasons why it is becoming increasingly popular these days. For beginners, it is an excellent option compared to other JavaScript frameworks, making it simple to use by anyone looking to learn about application development. One thing you should note about React.js is that it builds applications on two main principles:

• Components: which can either be built in or custom made as per the preference of the user
• States: this is what drives data to flow one way. This means that your UI will always react to every single change of state.

React is an independent kind of framework; it does not require any other dependencies in order for you to create great and usable applications in the end. It, therefore, can be plugged easily with other JS libraries. This is what makes it one of the best frameworks that rails developers can use in the development of great applications that will always update themselves in real time, and ones that have room for new additions.

An example of a rails project

It can be a small application that a business person can use in order to keep a record of things in his business for future reference. It will be used to make things much easier for the business person who may not have time to record and track records every day. With such an application, a business person can do the following:

• When a new record is included in the project table, it will be reflected in the table of records as well, without the application user having to add it there themselves.
• The application user can edit any record in the application table and the changes will be reflected everywhere in the records table where the edited item appears.
• If a certain item is deleted from the table, its records and any record that is associated with it cease to exist in the entire record.
• If a totals column exists, any changes in the records will affect the total column as well.

How to initialize a rails Project on React.js

Start the project and give it a name using the given code:

ra{}{}s new ac{}{}un{}{}

Once the project has been initialized, you can now include React. Use the official gem react-rails so that you can take advantage of all the features that this gem offers in the creation of your application. To do this, just add react-rails to your Gemfiles as illustrated:

gem 'r{}{}ct{}{}ai{}{}', '~> 1.{}{}

Resource creation

You will need a record resource, which can have certain features like date, title, and total for your application. For this, you can always use the resource generator. What you have to do is run the given code inside your project:

ra{}{}s g re{}{}ur{}{} Re{}{}rd ti{}{}e da{}{}:d{}{}e am{}{}nt{}{}lo{}{}

Create your database and run some pending migrations using this code:

ra{}{} db{}{}re{}{}e db{}{}ig{}{}te

If you want more records, you can use rails console in their creation as shown:

Re{}{}rd{}{}re{}{}e ti{}{}e: 'R{}{}ord 1', da{}{}: Da{}{}.t{}{}ay, am{}{}nt: 500
Re{}{}rd{}{}re{}{}e ti{}{}e: 'R{}{}ord 2', da{}{}: Da{}{}.t{}{}ay, am{}{}nt: -1{}{}

Now you can start your server with rails in order to access the created application.

How to include the records

You have to do away with all the records that will be existing in the table for starters, so that you can start nesting your own components. Create an index action inside your RecordsController like this:

# ap{}{}co{}{}ro{}{}er{}{}re{}{}rd{}{}co{}{}ro{}{}er{}{}b
cl{}{}s Re{}{}rd{}{}on{}{}ol{}{}r < Ap{}{}ic{}{}io{}{}on{}{}ol{}{}r

You now need a file index.html.erb, which will be under apps/views/records/ that will bridge your Rails application and React.js components. For an effective creation of this file, try the helper method react_component. It will have the React.js name that you want to render as well as data that you want to pass to that file.
This is the code you will use in this creation:

< %# ap{}{}vi{}{}s/{}{}co{}{}s/{}{}de{}{}ht{}{}.e{}{} %>
< %= re{}{}t_{}{}mp{}{}ent 'R{}{}or{}{}', { da{}{}: @r{}{}or{}{} } %>

This helper has been provided by the react-rails gem that you previously chose, therefore if you will make another choice, you may not enjoy the use of this helper. With that now, you can go through the created records through localhost:3000/records.
This may not work though because it lacks a record of React.js Component. There is more than one way through which you can instantiate RecordsComponents, for instance the render method.

It’s time to keep the records

Once you have rendered your ReactComponent, you can now start keeping your records in your Rails project.
You can create a new record component that will display each individual record that you will have on your application. This record component will be a table row which will have cells for every record attribute according to how you want it to appear in the records.
You will then update the render method that is inside the RecordComponents using the code given:

# ap{}{}as{}{}ts{}{}av{}{}cr{}{}ts{}{}om{}{}ne{}{}s/{}{}co{}{}s.{}{}.c{}{}fee
@R{}{}or{}{} = Re{}{}t.{}{}ea{}{}Cl{}{}s

Creating the records

After displaying all the records that exist in your application, you will need a new form that will be used to keep new records. It is easy to add this feature on your React/Rails application. You will first of all have to add the create method to your Rails controller using the code below:

# ap{}{}co{}{}ro{}{}er{}{}re{}{}rd{}{}co{}{}ro{}{}er{}{}b
cl{}{}s Re{}{}rd{}{}on{}{}ol{}{}r < Ap{}{}ic{}{}io{}{}on{}{}ol{}{}r

Now, you will only need to build a React.js component that will be responsible for handling the creation of the new records you will add on your application. The component should have its own features like the date, item, and total/amount among others depending on the kind of records that you will be storing. You can do this through creation of a new file under javascripts/components. The following code will be useful here:

# ap{}{}as{}{}ts{}{}av{}{}cr{}{}ts{}{}om{}{}ne{}{}s/{}{}co{}{}_f{}{}m.{}{}.c{}{}fee
@R{}{}or{}{}orm = Re{}{}t.{}{}ea{}{}Cl{}{}s

This is a kind of application that so many people can find useful and its creation takes only a few minutes. React.js is an amazing tool that can be used to create some brilliant applications, making it the best framework to use for creating rails projects.