Rails 5: How to create Ruby on Rails project (MySQL, Git)

Article presents how to initialize basic Ruby on Rails application (Rails 5) on Ubuntu 16.04 with MySQL database.

Platform:    Ubuntu 16.04
Ruby:         2.3.0
Rails:          5.0.0
MySQL:      5.7.13

 

Visit following post to see ow to install Ruby, Rails and MySQL: Ruby on Rails installation on Ubuntu 16.04

Creating new application

Create new application with MySQL database (my_app is a name of application):

This command creates a folder my_app containing a Rails project. Go into this project:

 

Setup MySQL

Before the application can be run, we need setup access to MySQL database. It requires to use database’s password and because we are going to use git later (which could be public), we store password in safe place. Open config/secrets.yml and add under development 2 key-value pairs:

In above, change db_user on your database’s user and db_password on its password. Now, open config/database.yml and add our secrets:

Next, we have to hide our secrets.yml before publication on GitHub or Bitbucket repository. Open .gitignore:

And add:

 

Next thing is to initailize project’s database in MySQL:

 

Now you should to be able to run Rails project without errors:

If you run your app on remote server (like VPS), probably you will need to use following command:

Replace XXX.XXX.XX.XX with IP of your remote server. You can change also port by setting proper -p.

Finally, open your app in web browser. By default it is:  localhost:3000. If you run on remote server, you need replace localhost with remote server’s IP. In web browser you should see:

Przechwytywanie

 

 

Git

The last thing is to setup git repository. We start by configuring git on server:

We use Bitbucket, but Github is very similar and you shouldn’t have any problem with it.

Go to Bitbucket and create new repository: Repositories > Creat repository. Choose name and use Git as Repository Type.

Bitbuc1

 

You can choose I’m starting from scratch and follow instructions from the box:

To test if everyting is fine, follow the instructions from second box:

Now, you can go open your Bitbucket repository in web browser and verify that you can find project’s sources there.

 

Git Workflow

Below is presented one out of the most common ways of team’s cooperation on repositories:

git_flow

 

 

Useful Git cheat-sheet from zeroturnaround.com :

 

Another useful cheat-sheet you can find on services.github.com

 

Tomasz Antas

Ruby on Rails developer and Web designer.
The area of his interest includes Popular Science, Internet of Things, Wearables, AI and Virtual/Augmented Reality.

Latest posts by Tomasz Antas (see all)

Thanks for reading!

  • Milou

    Thanks for this guide!

    I do get an error though.. and I can’t figure out what’s wrong. As soon as I

    $ rake db:create

    this is the error that I get:

    rake aborted!
    YAML syntax error occurred while parsing …….. Please note that YAML must be consistently indented using spaces. Tabs are not allowed. Error: (): did not find expected key while parsing a block mapping at line 14 column 3

    Do you have any idea what this could be? Following the error to line 14 is not helping ….

    • Tomasz Antas

      I think that problem is with spaces at the beginnings of lines in one out of your YAML files (secrets.yml or database.yml). In database.yml, probably you’ve got:

      default: &default
      adapter: mysql2
      encoding: utf8

      Before first line: “default: &default”, shouldn’t be any space (spacebar or tab). But each child element, like “adapter…”, should be placed after spaces (made by spacebar). The number of spaces should be the same for each child (“adapter…”, “encoding…” etc.). To fix it, you can remove intends for each child and add the same number of spaces for each child.

      You can use also some a some online YAML validator, like: http://www.yamllint.com/

      Another issue could be the lack of colons ( : ).

      Please, let me show your code if the problem will still exist.

  • Pingback: Top 10 From Around The Web: Ruby On Rails Resources – DailyNews Programming()