Some new updates in rails 6

Tram Ho

Recently, rails has just been updated to the 6th version with some major changes such as changing the JavaScript complier default to Webpacker instead of assest pipeline (Sprockets) and updating some more methods to help programmers work faster.

What is webpacker?

The first thing that comes to Rails 6 is webpacker being the most noticed, so what is webpacker? but are set as the default instead of sprockets. Webpacker is a packaged gem of webpackthe popular JavaScript tool used for managing and bundling JavaScript code , in short, Webpack is a tool to help package all js and css files (including scss, sass, ..).

Webpacker makes it easy to use webpack without having to configure it (but it is difficult to configure it more deeply, if you already know webpack) When you create a new rails rails new application you will see the output under the console. like this

And you will see how to install a lot of packages through yarn You check Gemfile you’ll see that the webpacker is installed by default.

Change where javascript code is not

The rails 6 javascript code version will be contained in app/assets/javascripts , but from rails 6 javascript code and the front end code will be managed at app/avascripts

There are 2 main directories, channels and packs . The channels folder is created by Action Cable and the pack folder is an important folder that will contain all javascript code, images, css, scss … Here I will not go into it, in the following articles I will find understand in more detail.

Add shortcut to the #pick method

Assuming you want to find the name of the user whose email is: [email protected] , you would write the following

In rails 6, you just need to rewrite the following:

This method is defined as follows:

Allows configuration of has_secure_password attribute

has_secure_password is used to encrypt and authenticate passwords using the Bcrypt gem when the model has the password_digest column

Prior to rails 6 has_secure_password would not accept any fields other than password_digest. If we want to use Bcrypt for other fields, we must redefine the encryption methods based on bcrypt and the authenticate function and store them.

Since rails 6 has_secure_password has been able to customize parameters and the default is still the password field and the self-defined field must be named in the form column_name_digest on the model. And has_secure_password is also added method authenticate_column_name to verify that column. We will see the following example

Negative of enum

When you declare enum a field of Rails model will add us some default scope to filter data by that value. For example

Rails 6 provides negative values ​​of these values ​​by adding the not_scope prefix


Above I have summarized some updates on rails 6 hope to help you. Happy Codding ~!


Share the news now

Source : Viblo