Setting up your RoR environment for success

Elena Weber
4 min readMay 16, 2021

Suppose you need to create a Ruby on Rails application and don’t know what to start with. Let this post be your guide.

  1. Install rails in your system — for that, just type:

$ gem install rails

in your terminal and hit Enter. The installation process will take a few minutes, so be patient with it, and let’s hope it’ll go smoothly and without any errors.

2. In the folder where your other app folders are, type in the terminal:

$ rails new my_new_app

where “my_new_app” is the name of your own app.

3. After rails has successfully created the folder and necessary files in it (there will be quite a lot), open this folder in your VS Code (or any other code editor) and type this in your terminal:

$ cd my_new_app

This will take you to your app’s folder in the terminal.

4. Open your GitHub account and choose “New” in your repositories. Enter the name for your repo, make sure you choose “public” and don’t check anything else — RoR has already created them for us (except for license).

Enter in your terminal:

$ git add .

$ git status

The first line will add all the files from the folder to your git repo, the second will show the current status.

5. Copy these lines from git repo and paste them into your terminal:

GitHub lines to copy and paste

What is going on here is the following:

Committing the very first commit for GitHub, creating the mail branch for the project, specifying the remote git repo and pushing (= sending) the commit to your GitHub repo.

6. Add the necessary gems to your Gemfile and run in your terminal:

$ bundle install

For example, for my project I had to uncomment ‘bcrypt’ (it was added to the original Gemfile by RoR) and add these:

Add necessary gems

7. Make sure your server is up and running — type:

$ rails s

in the terminal and try visiting this page in your browser:

http://localhost:3000

If you see a happy message, congrats! — you’ve successfully started the server.

8. Time to create MVC! I prefer using this command:

$ rails g resource user

It adds models, views, controllers, routes and migration files.

As an option, you can immediately create columns in your table like this:

$ rails g resource user username age:integer student:boolean group:belongs_to

Username, since its type is not specified, is a string (default), age is an integer, student is a boolean and our user belongs_to a group (it’s important to add that if this is the case because rails will create important additional lines to make proper connection between users and groups). Surely, you can make changes and add more columns in your migration files manually, no problem.

If you only need controllers, views and routes, type:

$ rails g controller user

If you only need models and migration files, type:

$ rails g model user

9. Add necessary information to models, views and controllers and run:

$ rails db:migrate

Now, all our tables are connected into a nice database (hopefully, properly). If you’ve made a mistake and it’s better to remigrate everything, first type this:

$ rails db:drop

this will delete the database as if it had never existed, so don’t forget to migrate everything once again after you’ve made corrections or adjustments.

10. Time to seed! I always seed my projects because it makes web development and catching errors so much easier!

11. Do proper OmniAuth setup and make sure it works. My code for the authorization on the website looks like this — it allows signing up and logging in through Facebook and through the app itself.

Authorization code

12. Add missing routes and views and error messages, set up sessions and users’ roles separation (for instance, a user cannot edit/delete other users’ content).

13. After your project works as expected it’s time to do some css styling, and voila!

PS Don’t forget to regularly push your changes to GitHub, it’s really important and very useful.

I hope this little guide will help you set up your environment for success. Happy coding! :)

--

--

Elena Weber

A former English teacher is now a Software Engineer exchanging human languages for computer ones :)