Skip to main content

Virtual Studio Code for Ruby on Rails Development

Introduction to Vscode (Visual Studio Code)


Visual Studio Code i.e. Vscode is a modern and robust code editor developed by Microsoft.
We can use it for setting up Ruby on Rails development environment.
It combines the simplicity of code editor with powerful tools like IntelliSense, code completion, debugging and built-in Git support. One of the beauties of Vscode it's free and open source.
It is available for Windows, Mac, and Linux with huge community support. 
How to Install Vscode?
Installation steps of Vscode are pretty simple, go to Visual Studio it will automatically detect the operating system you are using and provides a download link. After installation, you are ready to start customizing Vscode for Rails development.
How do you set up Vscode for Rails?
· Ruby extension:
The first thing you have to install the ruby extension in Vscode. Press f1, type ext install then search for ruby OR go to ‘View’ > ‘Extensions’ and search for ruby and hit install.
· Debugger:
    In ruby extension, there is ruby-debug ide protocol to allow Vscode to communicate with ruby-debug. It requires ruby-debug-ide to be installed on your system. This is how Ruby Mine/Net Beans does by default. Read the documentation GitHub to install ruby-debug-ide protocol.
· Linters:
Linting is the process of running a program that will analyze code for potential errors. Below is the list of linters supported by ruby. You will need to install the ruby gems for each of these for Linting to work:
1. rubocop
2. ruby-lint
3. reek
4. fasterer
5. debride
After installing ruby gems for linters, enable each one in your workspace or user settings. To do this go to ‘File’ > ‘Preferences’ > ‘Settings’ and paste the below settings.
 "ruby.lint": {
      "reek": true,
      "rubocop": true,
      "ruby": true,
      "fasterer": true,
      "debride": true,
      "ruby-lint": true
},
· Ruby on Rails snippets extension:
Ruby on Rails snippets extension save your lots of time. After installation reload Vscode and just type ‘controller-rest’ it automatically writes all the rest methods for you. It will save your 80% time. There are many snippets available for controllers, models, and views. Go ahead try yourself.
· Ruby Solargraph:
Solargraph is a language server that provides IntelliSense, code completion, and inline documentation for Ruby.
Themes and Editor Settings
· Theme:
Vscode provides a couple of built-in themes. If you are a sublime user, just go to ‘File’ > ‘Preferences’ > ‘Color Theme’ and select ‘Monokai’ theme. There are hundreds of themes available for you, just go to ‘File’ > ‘Extensions’ and type theme name and install it.
· Font:
The font is one of the most important things when you are writing code. If the font of your code editor doesn’t looks good you cannot write code for a long time and it hurts your eyes.
'. Download it and install on your system. Go to FIRA CODE. There is beautiful font available for you that is ‘Vscode settings and add below settings to activate ‘FIRA CODE’.
"editor.fontFamily": "Fira Code",
"editor.fontLigatures": true,
 · Editor settings:
These are some editor settings that you can prefer.
"editor.lineHeight": 40,
"editor.rulers": [
80,
120
],
"[html]": {
"editor.foldingStrategy": "indentation"
},
"html.format.indentInnerHtml": true,
"editor.tabSize": 2,
"editor.formatOnSave": true,
"editor.autoIndent": true,
"editor.fontWeight": "500",
"html.suggest.html5": true,
"editor.fontSize": 17,
"editor.minimap.maxColumn": 30,
  
Task Automation:
Lots of tools available to automate tasks like Linting, code formatting, and testing. These tools uses command line interface to run the tasks. But we want everything in our editor, we don’t want to switch from editor to terminal for running single command.
To create tasks in Vscode click on ‘Task’ > ‘Configure Task’ > ‘Create task.json file form template’ > ‘Others’ it will create ‘.vscode’ directory into root of your project and also creates ‘task.json’ file. Below file generated by Vscode.
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "echo",
"type": "shell",
"command": "echo Hello"
}
]
}
 The task array contains all your tasks. Read more about task automation in Vscode. It makes developer life easier.
We have created a couple of tasks for htmlbeautifier, Rubocop, rspec, rails server etc. You need to install the htmlbeautifier gem to use ‘beautify’ task.
 "version": "2.0.0",
"tasks": [
{
"taskName": "beautify",
"type": "shell",
"command": "htmlbeautifier ${relativeFile}",
"problemMatcher": [],
"presentation": {
"reveal": "never"
}
},
{
"taskName": "rubocop",
"type": "shell",
"command": "rubocop --auto-correct ${relativeFile}",
"problemMatcher": [],
"presentation": {
"reveal": "never"
}
},
{
"taskName": "rspec",
"type": "shell",
"command": "bundle exec rspec",
"problemMatcher": [],
"presentation": {
"reveal": "always"
}
},
{
"taskName": "rspec file",
"type": "shell",
"command": "bundle exec rspec ${relativeFile}",
"problemMatcher": [],
"presentation": {
"reveal": "always"
}
},
{
"taskName": "rails c",
"type": "shell",
"command": "bundle exec rails console",
"problemMatcher": [],
"presentation": {
"reveal": "always"
}
},
{
"taskName": "rails s",
"type": "shell",
"command": "bundle exec rails server",
"problemMatcher": [],
"presentation": {
"reveal": "always"
}
}
 So here it is folks, you can create many tasks as per your requirement. If you need any help for setting up Vscode for Ruby on Rails development please comment below :)

Comments

Post a Comment

Popular posts from this blog

GraphQL With Ruby

Now a day’s most of the web or mobile applications fetch data from server which is stored in a database. REST API provides an interface to stored data that require by the applications. GraphQL is a query language for REST API's not for server databases. It is database agnostic and effectively can be used in any context where an API is used. GraphQL provide platform for declarative data fetching where client need to specify what data needs from API in response. Instead of multiple endpoints that return fixed data structures, a GraphQL server only exposes a single endpoint and responds with precisely the data a client asked for. GraphQL minimizes the amount of data that needs to be transferred over the network and improves applications operating under these conditions. Introduction to GraphQL API on Ruby on Rails Start with adding gem in Gemfile gem ‘graphql’ Run command bundle install Run command rails generate graphql:install Above command will add gr

Best In Place Gem In Ruby On Rails Tutorial

The best_in_place gem is the easiest solution for in place editing in Ruby on Rails. This gem provides functionality of “in place editing” in ruby on rails without writing any extra ajax code. It supports text inputs, textarea, select dropdown, checkboxes, jQuery UI Datepickers, etc. Also Displays server-side validation Installation Steps of “best_in_place” Gem : Installing best_in_place is very easy and straight-forward. Just begin including the gem in your Gemfile: gem ‘best_in_place’ After that, specify the use of the jquery and best in place javascripts in your application.js, and optionally specify jquery-ui if you want to use jQuery UI datepickers: //= require jquery //= require best_in_place //= require jquery-ui //= require best_in_place.jquery-ui Then, just add a binding to prepare all best in place fields when the document is ready: $(document).ready(function() { /* Activating Best In Place */ jQuery(".best_in_place").best_in_place(); });

Data scraping in Ruby on Rails using Nokogiri and Mechanize Gem

What is Data scraping? Website/Data  Scraping  is a technique to operating large amounts of  data  from websites whereby the  data  is extracted and displayed in own sites or it can be stored to a File/Database. Data scraping is basically used where the websites does not provides API. Some Applications do not provide API to collect records. For the same , Data Scraping technique is used. The data can be scraped using Nokogiri Gem. The steps are required: Add the gem “gem ‘nokogiri’, ‘~> 1.8’, ‘>= 1.8.1'” . Then run the bundle install Add the “require ‘nokogiri'” , “require ‘open-uri'” line where you will write the code for the scraping. The controller of the page will look like below: The view of the code of view page will look like : The result in our application will look like: Mechanize Gem in rails The Mechanize library is used for automating interaction with websites. Mechanize automatically stores and sends cookies, follo