Codementor Events

Getting Started with Ruby on Rails, PostgreSQL, and Heroku Deployment under 10 minutes!

Published Aug 26, 2023
Getting Started with Ruby on Rails, PostgreSQL, and Heroku Deployment under 10 minutes!

Ruby on Rails is a popular web application framework that allows developers to build robust and dynamic web applications quickly. In this tutorial, we will guide beginners through the process of creating a new Ruby on Rails application, setting up a PostgreSQL database, and deploying the application to Heroku, a cloud platform-as-a-service (PaaS) provider.

Prerequisites

Before you begin, make sure you have the following tools and accounts set up:

Ruby: Make sure you have Ruby installed on your machine. You can check by running ruby -v in your terminal.

RubyGems: RubyGems is Ruby's package manager. Check if you have it installed by running gem -v.

Rails: Install Ruby on Rails using the command gem install rails.

Git: You'll need Git installed to manage version control.

Heroku Account: Sign up for a free Heroku account at https://www.heroku.com/.

Heroku CLI: Install the Heroku Command Line Interface to interact with your Heroku account via the terminal.

PostgreSQL: Install PostgreSQL, which will be used as the database for your Rails application.

Step 1: Creating a New Rails Application

Open your terminal and navigate to the directory where you want to create your new Rails application. Run the following command to generate a new Rails app:

rails new my_rails_app

Step 2: Configuring the Database

By default, Rails uses SQLite as the database. However, we want to use PostgreSQL. Open the config/database.yml file in your Rails app directory and update the configuration for the development and production environments to use PostgreSQL:

development:
  <<: *default
  adapter: postgresql
  encoding: unicode
  database: my_rails_app_development
  pool: 5
  username: postgres
  password:

production:
  <<: *default
  adapter: postgresql
  encoding: unicode
  database: my_rails_app_production
  pool: 5
  username: postgres
  password:

Make sure to adjust the database names and credentials as needed.

Step 3: Creating the Database

Run the following commands to create the databases:

rails db:create
rails db:migrate

This will create the databases specified in the database.yml file and run the initial migrations.

Step 4: Creating a Simple Route and View

Let's create a simple route and view to test our application. Open the config/routes.rb file and add the following line to define a route:

get 'welcome/index'

Next, create a corresponding controller and view. Run the following commands:

rails generate controller Welcome index

Open the app/views/welcome/index.html.erb file and add some HTML content to test the view.

Step 5: Testing Locally

Start the Rails server using the command:

bundle exec rails server

Open your web browser and navigate to http://localhost:3000/welcome/index to see your view in action!

Step 6: Deploying to Heroku

Now, let's deploy our Rails application to Heroku.

**1. **Login to Heroku: Run heroku login and enter your Heroku credentials.

**2. **Initialize Git: If you haven't already, initialize a Git repository in your Rails app directory using git init.

**3. **Create a .gitignore file: Create a file named .gitignore in your app's root directory and add the following lines to ignore unnecessary files:

.bundle
log/*.log
tmp/**/*
node_modules/
public/assets

4. Commit Your Code: Add and commit your code to the Git repository.

5. Create a Heroku App: Run heroku create to create a new Heroku app.

6. Push to Heroku: Deploy your code to Heroku using git push heroku master.

7. Migrate Database: Run the migrations on the Heroku server using heroku run rails db:migrate.

8. Open the App: Visit your newly deployed app by running heroku open.

Congratulations! You've successfully created a Ruby on Rails application, configured a PostgreSQL database, and deployed the Rails app to Heroku!

Remember that this guide covers just the basics. As you delve deeper into Rails and Heroku, you'll discover more features and optimizations to enhance your application further. Happy coding!

Discover and read more posts from K M Rakibul Islam
get started
post comments2Replies
Free vote
7 months ago

Capstone Project is a haven for amateur enthusiasts like me. With its vast array of topics and supportive community: research proposal writing service, it’s more than just a learning platform—it’s a gateway to endless possibilities. Exploring here ignites my curiosity and fuels my passion for discovery. An invaluable resource for any eager learner

Free vote
8 months ago

I can’t say enough about the remarkable dissertation proposal writing services I received from this provider. They truly exceeded my expectations in every way. The team’s knowledge and expertise in this specialized area were evident throughout the process. They took my research concept and transformed it into a well-structured, persuasive proposal: research proposal writing services Their commitment to quality and attention to detail were exceptional. Thanks to their assistance, I have a solid foundation for my dissertation that gives me confidence in the research journey ahead. If you’re in search of top-tier support for your dissertation proposal, look no further – these services are second to none.