Ruby on Rails Getting started with Ruby on Rails Creating a Ruby on Rails Application

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

This example assumes Ruby and Ruby on Rails have already been installed properly. If not, you can find how to do it here.

Open up a command line or terminal. To generate a new rails application, use rails new command followed by the name of your application:

$ rails new my_app

If you want to create your Rails application with a specific Rails version then you can specify it at the time of generating the application. To do that, use rails _version_ new followed by the application name:

$ rails _4.2.0_ new my_app 

This will create a Rails application called MyApp in a my_app directory and install the gem dependencies that are already mentioned in Gemfile using bundle install.

To switch to your newly created app's directory, use the cd command, which stands for change directory.

$ cd my_app

The my_app directory has a number of auto-generated files and folders that make up the structure of a Rails application. Following is a list of files and folders that are created by default:

File/FolderPurpose
app/Contains the controllers, models, views, helpers, mailers and assets for your application.
bin/Contains the rails script that starts your app and can contain other scripts you use to setup, update, deploy or run your application.
config/Configure your application's routes, database, and more.
config.ruRack configuration for Rack based servers used to start the application.
db/Contains your current database schema, as well as the database migrations.
Gemfile Gemfile.lockThese files allow you to specify what gem dependencies are needed for your Rails application. These files are used by the Bundler gem.
lib/Extended modules for your application.
log/Application log files.
public/The only folder seen by the world as-is. Contains static files and compiled assets.
RakefileThis file locates and loads tasks that can be run from the command line. The task definitions are defined throughout the components of Rails.
README.mdThis is a brief instruction manual for your application. You should edit this file to tell others what your application does, how to set it up etc
test/Unit tests, fixtures, and other test apparatus.
temp/Temporary files (like cache and pid files).
vendor/A place for all third-party code. In a typical Rails application this includes vendored gems.

Now you need to create a database from your database.yml file:

5.0
rake db:create
# OR
rails db:create
5.0
rake db:create

Now that we've created the database, we need to run migrations to set up the tables:

5.0
rake db:migrate
# OR
rails db:migrate
5.0
rake db:migrate

To start the application, we need to fire up the server:

$ rails server
# OR
$ rails s

By default, rails will start the application at port 3000. To start the application with different port number, we need to fire up the server like,

$ rails s -p 3010

If you navigate to http://localhost:3000 in your browser, you will see a Rails welcome page, showing that your application is now running.

If it throws an error, there may be several possible problems:

  • There is a problem with the config/database.yml
  • You have dependencies in your Gemfile that have not been installed.
  • You have pending migrations. Run rails db:migrate
  • In case you move to the previous migration rails db:rollback

If that still throws an error, then you should check your config/database.yml



Got any Ruby on Rails Question?