generaldiscourse

Create a new Rails application

To create a new Rails application, initialize a new project as shown below.

  • First, create a new folder where to store your Rails projects, such as the /opt/bitnami/projects directory, and give write permissions for the current system user:

      $ sudo mkdir /opt/bitnami/projects
      $ sudo chown $USER /opt/bitnami/projects
    
  • Then, to create a new Rails application with an SQLite database, initialize a new project as below. Remember to replace the APPNAME placeholder with the actual name of your project:

    • SQLite database (default):

        $ rails new APPNAME
      
    • MariaDB or MySQL database (recommended):

        $ rails new APPNAME --database mysql
      
    • PostgreSQL database:

        $ rails new APPNAME --database postgresql
      

Configure database credentials

If you are using a database other than SQLite, you will need to configure the database credentials. Edit the default section in the config/database.yml file inside your Rails project directory as shown below:

default: &default
  adapter: mysql
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password: PASSWORD
  host: localhost

NOTE: Replace the PASSWORD placeholder with your database password. Learn how to obtain the MariaDB database credentials.

Create application database and initialize the schema

To create the application database and initialize the schema for your Rails application, navigate to your project directory and run the following command:

$ bundle exec rails db:prepare

Add a welcome page

By default, Rails does not add any view to your application. To add a sample view for your application, follow the steps below:

  • Create the Welcome controller with the index action as below:

      $ rails generate controller Welcome index
    
  • Edit the config/routes.rb file as shown below:

      Rails.application.routes.draw do
        root 'welcome#index'
        # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
      end
    

Test your Rails application

The Rails application can be started by using this command, and it will run on port 3000:

$ bundle exec rails server

To access the application, browse to http://SERVER-IP:3000/. To end the application, terminate the running Rails process.

Last modification July 29, 2020