At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

, , ,

Rails 6.0 Features – The Top 3 Great Features of the latest rails version

  • By Guru Moorthy
  • September 13, 2019
  • 1192 Views

Rails is a web application development framework that is written in a language called ‘Ruby’. This language allows you to write shorter codes without compromising its functionality. While accomplishing more than many other languages and frameworks, now the release of new version Rails 6.0.0 with more useful features is trending among techies.
rails 6 implementationSo, this blog unveils the main features of latest rails 6.0 version.
Upgrading Rails App to 6.0
Action text
Action Mailbox
Multiple Databases with Active Record

Upgrading Rails 5.2 to 6.0

Rails 6 requires Ruby 2.5.0 or newer to update. We need to update the ruby and rails version respectively, and then you update in “gemfile”. Now, run this command.

bundle update.

You can use this task to update the rails app

rails app:update

You may need to change many things manually.
They are Config.load_defaults value in “config/application.rb”.
Then, add “webpacker” gem into our application.

 gem "webpacker"

Run the following command

rails webpacker:install

The above command creates a separate folder for javascript. We need to migrate all our existing javascript to it.
If you were previously loading any of the actioncable, activestorage, or rails-ujs, it will now load packages through npm/yarn.
Webpacker, besides being the default JavaScript compiler for Rails 6, it does not get activated by default when upgrading. So, when using Webpacker, include it in your Gemfile and then install it.
Once all npm package is moved to @rails scope, you got to update the names of the following dependencies.

actioncable   → @rails/actioncable
activestorage → @rails/activestorage
rails-ujs     → @rails/ujs

If this looks like a long process for you, just create the new rails 6.0 app and migrate your code. And you can use yarn add “any lib” to do this.
Here, let us consider an example for a clearer understanding of the features. Creating an app for an email campaign would be a great concept. Using action text, we are going to draft a newsletter and send it to the subscriber. The subscriber can able to reply through the email and that is going to be captured with help of action mailbox and add to the comments table. Here, a separate database is created for comments to implement the Multiple Databases with Active Record feature.
So now you can create the new application and models with the following commands.

rails new rails6_implementation 
rails generate scaffold Newsletter subject:string from_address:string 
rails generate scaffold Subcriber name:string email:string

Make DB config and migrate the app.
Now add action text field “content” in the newsletter model.

Action text

Action Text brings rich text content and editing to Rails. It includes the Trix editor which handles all of your actions like adding image, text, link and others to the content. Following is the code to install.

rails action_text:install

It installs active storage if that’s not present in your application.

has_rich_text :content

You need to add the above line to your model to add rich text field.

<%= f.rich_text_area :content %>

Place the above code in the ‘view’ section.
Furthermore, permit the params in controller

:content

To change the design, you need to remove the default one.
you need to remove the app/assets/stylesheets/actiontext.scss linker and base your stylings on the contents of that file. Get additional information here.
Here, the email configuration to send an email for the subscriber is done using “mailgun”, you can do with your preferred mail service.
Now, proceeding to capture the reply text from email by the subscriber, add the “reply_to” field when sending an email. Add the newsletter id to track user reply to which the newsletter goes.
Rails 6 features

Action Mailbox

Action Mailbox routes incoming emails to controller-like mailboxes for processing in Rails. It ships with ingresses for Mailgun, Mandrill, Postmark, and SendGrid. You can also handle inbound mails directly via the built-in Exim, Postfix, and Qmail ingresses.

rails action_mailbox:install
rake db:migrate

This will also generate active storage migration and action mailbox. And create file application_mailbox.rb in mailboxes.
Here, you can define your routes, and then create the mailbox for replies likewise the code given below.

rails g mailbox Replies

This code creates the replies mailbox for the further use. There, you can find a process method and get access to the “mail” object.
You can access all the mail parsing methods of mail gem, because it is built on top of this.
rails 6 features
Above is the mailbox class, where we are processing the inbound mail. To capture your mail, configure your domain in MATCHER.

For testing in local

Start your rails server, enter the URL given below. Here you will get a form to test your mailbox.

http://localhost:3000/rails/conductor/action_mailbox/inbound_emails/new

If you need to test with your actual mail, you need to set up server or ngrok(local). Then, configure routes in “mailgun”.

http://{YOURDOMAIN}/rails/action_mailbox/mailgun/inbound_emails/mime


Add comments table in separate database by using the Multiple Databases with Active Record feature in rails 6.0

Multiple Databases with Active Record

This feature becomes almost inevitable when you are scaling up your application on a database level. The latest version aids you by supporting multiple databases to store your data. With the multiple database structure, you do not need to rely on one single database which eventually enhances the speed when retrieving data from the database.
Firstly, you need to add the new database config in database.yml file.

development:
  primary:
    <<: *default
    database: rails6_implementation_dev
  primary_replica:
    <<: *default
    database: rails6_implementation_dev_replica
    replica: true
  primary2:
    <<: *default
    database: rails6_implementation_dev_2
    migrations_paths: db/primary2_database

Here, add multiple databases for the development. You need to use replica: true for creating the database replication.
The “migrations_paths” is used to fix the location of the migration files for a particular database.

To create the model or migration on a particular database

The following commands allow you to create a model and migration on a particular database respectively.

rails g model comment --database primary2
rails g migration CreateComments  --database primary2

To migrate specific database

rake db:create:primary2
rake db:migrate:primary2

Now, comment model is created on database primary2 by using the above command. Before configuring in your model, you can’t access the active record.
rails 6 implementation
So, the above picture shows that the comments database is not connected via model.
For connecting with active record, add the following to your model

connects_to database: { writing: :primary2, reading: :primary2}
connects_to database: { writing: :primary, reading: :primary_replica}

Once the model is connected, now you can check in rails console which will return the record as shown below.
rails 6 implementation
You can refer to my Github repo to get the complete code. Do share your thoughts about the most interesting feature that you felt in rails 6 below in the comments.
Happy coding!
Hire best Ruby on rails developers for your web development projects easily with Agira technologies. We provide you with dedicated experts who have mastered the latest technologies. Endure the fullest of outsourcing tech experts for your company. Get in touch with our experts to hire freelance python developers, offshore python developers and python web developers who can cater to your company’s requirement.
Python web development
Read more blogs related to Ruby on rails and other latest tech news. Such as Active Storage In Rails 5.2 With Real Time Example and How to Learn Ruby on Rails from Scratch – Beginners Guide!
[contact-form-7 404 "Not Found"]