How to Run Ruby/Rails Commands from the IDE instead of the Terminal
In the Ruby/Rails community, using a terminal is the most popular way to perform various commands and tasks. These tasks include running scripts, tests, Rails generators, Git and Docker commands, and many other things. We also noticed this ourselves when we did some RubyMine usability testing among developers at RailsConf 2019. We found that most participants ran Bundler commands and tests from the IDE terminal.
In this blog post, we’ll discuss the RubyMine features that can be used instead of running specific terminal commands and the benefits that working with the IDE provides. We’ve divided the commands into several groups to allow you to quickly find a corresponding RubyMine feature:
Ruby
ruby script.rb |
There are multiple ways to run Ruby scripts in RubyMine:
All of the above actions create the Ruby run configuration, which stores all the passed parameters. You can customize these parameters, add new ones, and save the configuration to quickly run a script in the future. Learn more from the Run Ruby scripts topic in the documentation. |
irb |
To use the IRB console from inside the IDE, choose one of the following courses of action:
You can learn more from the IRB console section of the documentation. |
Ruby version managers
rvm use 2.6.3 rvm use 2.6.3@teddy rvm gemset use teddy rbenv shell 2.6.3 rbenv local 2.6.3 etc. |
Ruby version managers allow you to switch between several versions of Ruby in a terminal. RubyMine detects installed interpreters automatically and enables you to select the desired version in the following ways:
You can learn more from the Configure a Ruby interpreter section of the documentation. |
rvm gemset create teddy |
To create an RVM gemset, go to the Language & Frameworks | Ruby SDK and Gems page, right-click the required version of Ruby, and select New RVM gemset. You can also create a gemset when you generate a new project. |
rbenv gemset create 2.6.3 teddy |
If you have rbenv-gemset enabled in your project, RubyMine synchronizes the gemsets enabled in the Ruby SDK and Gems page with the ones specified in the .rbenv-gemsets file. So, you can create, enable, and disable rbenv gemsets in two ways:
You can learn more from the SDK gemsets section of the documentation. |
Note that starting from v2020.3, changing the Ruby SDK in RubyMine will automatically change the version of Ruby used in the RubyMine terminal [RUBY-22329].
Bundler
bundle bundle install bundle update etc. |
RubyMine integrates with Bundler, so you can install gem dependencies in the following ways:
You can find more about integration with Bundler in the Bundler section of the documentation. |
bundle exec [command] (for example, bundle exec rspec spec ) |
To run a command in the context of the bundle in RubyMine, you need to adjust the settings of the corresponding run configuration (for example, RSpec). In the Run/Debug Configurations dialog, select the required configuration, open the Bundler tab, and enable the Run the script in the context of bundle (bundle exec) option. |
bundle gem [gem_name] |
In RubyMine, you can generate a project skeleton for creating your own Ruby gem. On the Welcome screen, click New Project and select Gem in the Ruby group on the left pane. You can learn more from the Create and publish your first Ruby gem tutorial. |
Rake
rake db:create rake db:migrate etc. |
You can run Rake tasks from the IDE in two ways:
As always, you can customize and save parameters for running a Rake task in the created Rake run configuration. |
Rails
rails new my_app rails new my_api --api etc. |
In RubyMine, you can create different types of Rails applications: regular applications, Rails APIs, and mountable engines. On the Welcome Screen, click New Project and select the desired application type from the Rails group on the left pane. Here you can specify the version of Rails, choose the required template, or specify a JavaScript library to be used. |
rails g controller rails g model rails g scaffold etc. |
RubyMine provides dedicated dialogs for creating Rails elements, such as models, controllers, and so on. There are two ways to generate a Rails entity:
Learn more from Create Rails application elements. |
rails server |
To run a Rails application from RubyMine:
|
rails console |
RubyMine has a built-in Rails console so you can interact with your application without leaving the IDE. To open it:
|
rails db:create rails db:migrate etc. |
To run Rails database migrations in RubyMine, use the corresponding Rake tasks. |
rails test rails test user_test.rb etc. |
Learn how to run Minitest tests in your Rails application in the next section. |
Tests
Rakerake test rake test:models rake test:controllers etc. |
Running tests using Rake tasks works the same way as described in the Rake section:
|
Minitestruby -Itest user_test.rb
|
RubyMine allows you not only to run specific Minitest tests but also to run all tests in a given directory.
You can learn more about running tests from the IDE from Perform tests section of the documentation. |
RSpecrspec spec
|
Running RSpec tests from RubyMine works the same as running Minitests.
Note that RubyMine runs tests in the context of the bundle automatically. |
spring rspec spec |
To run tests with the Spring preloader, you need to customize a corresponding run configuration (RSpec, Test::Unit/Shoulda/MiniTest, or Cucumber). In the Run/Debug Configurations dialog, set the Use pre-load server option to Spring. |
Rubocop/Standard
rubocop rubocop app rubocop --auto-correct etc. |
RubyMine provides two ways to use RuboCop:
Learn more from the RuboCop topic. |
standardrb standardrb app standardrb --fix etc. |
Working with the Standard gem is similar: all you need to do is enable the Standard option on the Settings/Preferences | Editor | Inspections page for the RuboCop inspection. |
Capistrano
cap install cap staging deploy cap production deploy etc. |
To run Capistrano tasks in RubyMine, do one of the following things:
You can learn more from the Deploy a Rails app using Capistrano tutorial. |
Gem commands
RubyMine has a variety of dedicated run configurations for a wide range of Ruby and Rails tools. But there are hundreds of other useful gems and libraries. If you need to execute a command of an arbitrary gem, try to use the Gem Command run configuration. For example, RubyMine does not have a dedicated run configuration for executing Foreman commands. You can start Foreman commands (for example, foreman start
) by creating a Gem Command with the required argument. Learn more from Debug an application running with Foreman.
The features we’ve mentioned above don’t come close to capturing all of RubyMine’s capabilities. There are still many useful IDE features that are not related to Ruby/Rails directly, for example:
-
Integration with different Version Control systems allows you to use shortcuts for frequently used commands, such as
git checkout
,git pull
,git merge
, etc. -
Docker support allows you to manage images, containers, and services in a dedicated tool window instead of executing
docker pull
,docker-compose up
, and other commands in a terminal.
That’s about it for now. As always, you can use our issue tracker, Twitter, or the comments section below to get in touch with us and let us know if you need help with the IDE or if you would like to suggest any new features.
Cheers,
Your RubyMine team