10 helpful tips and tricks for Ruby On Rails Developers

Tram Ho

Working with Ruby On Rails will actually be even smoother if you follow these simple effective tips & tricks while developing your application. So developers, start taking note of these 10 useful Ruby On Rails tips & tricks to help you along the way.

1. Don’t put too much logic in the controller

Rails is based on the MVC architecture. Maintain a skinny controller is one of the most important things to increase readability and code testing.

For example, you have a controller in which the index function extracts a list of red and white shirts.

To identify these queries more specifically. Now, migrating this code to a database called Shirt model , to extract the red and white colors. Hope you can see how the controller’s index function will be written more easily.

Always follow the second approach when you face database queries in controllers.

We have a lot of logic, but these are the best examples we can use in controllers.

  1. Logic for handling cookies and sessions.
  2. Logic to find the correct model to perform DB operations.
  3. Logic to render results based on request type like HTML, XML, JSON, etc. or redirection.
  4. Logical to collect parameters and use that parameter to perform operations based on your request.

2. Don’t put too much logic in the view

ERB is a great way to build your views by embedding ruby ​​into HTML. However, you need to be very careful while building your views, large view files are difficult to manage and maintain. This is also an area that is likely to face code repetition and sometimes leads to violations like DRY (code should not be repeated).

Some of Rails best practices: Always try to use helpers in views Use layouts and partials when you feel the same code repeats many times in views Use presenters / decorators like Draper gem, this will help you reduce the line of code in your views files.

3. Do not put too much logic in the model

Some functions, such as creating email notifications, interfering with external services, are not related to the core responsibility of the Active Record model. The main function of Active Record is to find and perform CRUD operations in the database.

And some of the best logic we can use in Model:

  1. Active Record relations (Associations) and validations.
  2. Simple functions will update properties and store them in the database.
  3. Access Wrappers to hide internal model information (for example, the full_name method combines the first_name and last_name fields in a database).
  4. Database-related queries – Always try to avoid using any Active Record queries outside the model.

Literally, I was told not to use too much logic anywhere in MVC. At this stage, can you stop and think like where else can we put our logic? You are free to use your own logic but should not use it inside MVC. Instead, you can use all the logic outside of MVC, this will not affect the performance of the application and help us easily manage the logics.

4. Do not use too many Gems

Always remember that each gem you add into your application may have dependencies on other gems.

Using a large number of gems makes the size of your Rails application larger than necessary. This can slow down the performance of applications on production environments. This can also result in larger server memory configuration and increased operating costs.

So always be careful and cross-check while adding gems to your application.

5. Always check your application logs

Because all Ruby on Rails developers know that in rails there is a default file log available during development and on production, most developers will ignore the information in these files, it’s important to consider. Your log file during development and testing of your application so you can track process flow.

For example, you will often encounter N + 1 query problems and the only way to identify this N + 1 query problem is by reviewing your application log.

Reviewing log files is a great way to find inefficiencies in your code.

6. Always write Automated Tests

There should be at least one high-level test case written for each action in your controllers. At some point in the future, if your application is expanded, modified, or upgraded to the latest version of ROR, this testing framework will provide a clear way to verify its basic functionality. your. You can also find some defects in your logic by writing test cases.

7. Background Tasks

We may have already integrated third-party services into our applications via their API calling gems, but we cannot guarantee that it will always run normally, if the service catches up. What happens when the head runs very slowly?

To avoid blocking these calls, you can do them as background tasks, instead of calling these services directly as a normal request in your rails application.

Here are some popular gems used for this purpose:

  1. Delayed Job
  2. Sidekiq
  3. Resque

8. Use Slim or Haml Templating Language for views

By default, Rails uses the Templating ERB system, which allows you to embed Ruby code into HTML. If possible, always try to use Slim or lighter HAML than ERB. In addition, this Slim or HAML syntax is very easy when compared to ERB. Slim very fast, lightweight templating engine. By using this, you can achieve code cleanliness, more readability, and it can also reduce your response time. In addition, this also helps build your front-end very quickly.

9. Some useful gems to streamline your code and to boost your application performance

  1. Bullet – Gem strongly increases application performance, identifies N + 1 query problems and displays warning messages on your browser.
  2. Traceroute – It identifies unused routes in your application.
  3. Rails Best Practices – It checks the quality of the rails application’s code and provides recommendations.
  4. Dead-weight – It identifies unused CSS selectors.

10. Some helpful tips on Ruby On Rails Tips

Indexing: Database indexing is one of the easiest ways to improve database performance. It will enhance finding data from the database. Views: Never call DB related queries from your views, it will significantly affect your application performance. Controller variables: All dynamic data you are using in views must be defined by controllers. SAAS: Always write style (CSS) according to SAAS approach, this will always prevent you from overwriting the style. Content tag: Avoid content_tag in helpers, instead call them in partials. Helpers: Rails generate by default a helper for each controller. Delete them all and use helpers like form helper, links helper, menu helper, image helper, etc.

Refer

https://www.agiratech.com/10-useful-tips-for-ruby-on-rails-developers/

Share the news now

Source : Viblo