Creating and Running Your First Rails Application
This tutorial will show you how to create and run the most simple Rails project in IntelliJ IDEA.
Before starting this tutorial, do the following:
Download and install the Ruby distribution for your platform.
Install and set up IntelliJ IDEA.
Install the Ruby plugin.
We'll perform all steps using IntelliJ IDEA installed on macOS.
Create a Rails application
To create a Rails application from scratch, do the following:
-
Run IntelliJ IDEA and click Create New Project on the Welcome Screen.
-
In the New Project dialog, select Ruby on Rails on the left pane. Then, select the required Ruby interpreter in Project SDK and choose the Rails version. Click Next.
-
On the next page, specify the project name and location. Click Finish.
-
IntelliJ IDEA will create a new Rails application and installs the gem dependencies mentioned in Gemfile. You can see this process in the Run tool window.
-
After installing all dependencies, you can see a project structure in the Project tool window on the left. On the right, IntelliJ IDEA automatically opens main project files in the editor.
Start a web server
Now we have a functional Rails application already. To see it in action, you need to start a web server. Perform the following steps to do this:
-
Press Ctrl twice and start typing development.
-
Select the Development run configuration from the list and press Enter.
-
IntelliJ IDEA will show the process of preparing the application to run.
-
Copy the 0.0.0.0:3000 address used by a web server, insert it to the browser’s address bar and press Enter to see the Rails default information page.
Create a controller and view
Now let’s create a new page. To do this, you need to create a controller and a view.
-
Press Ctrl twice and start typing controller. In the invoked list, select rails g controller and press Enter.
-
In the invoked Add New Controller dialog, set the controller name to Welcome and add one action called index. Click OK.
-
IntelliJ IDEA will create a controller, view, and several other files. This process will be displayed in the Run tool window. In this window, click the index.html.erb file.
-
In the opened index.html.erb file, delete all of the existing code in the file, and replace it with the following single line of code:
<h1>Hello, Rails!</h1> -
To see the result, hover your mouse pointer over the view code, and then select the desired browser from the pop-up.
-
The index page will be opened in a selected browser.