Ruby on Rails


This draft deletes the entire topic.

expand all collapse all

Examples

  • 21

    This example assumes Ruby and Ruby on Rails has already been installed properly.

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

    $ rails new my_app
    

    If you want to define your rails version at the time of generating the application, 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 this 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
    

    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 are 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

  • 8

    To generate a controller (for example Posts), navigate to your project directory from a command line or terminal, and run:

    $ rails generate controller Posts
    

    You can shorten this code by replacing generate with g, for example:

    $ rails g controller Posts
    

    If you open up the newly generated app/controllers/posts_controller.rb you'll see a controller with no actions:

    class PostsController < ApplicationController
        # empty
    end
    

    It's possible to create default methods for the controller by passing in controller name arguments.

    $ rails g controller ControllerName method1 method2
    

    To create a controller within a module, specify the controller name as a path like parent_module/controller_name. For example:

    $ rails generate controller CreditCards open debit credit close
    # OR
    $ rails g controller CreditCards open debit credit close
    

    This will generate the following files:

    Controller: app/controllers/credit_cards_controller.rb
    Test:       test/controllers/credit_cards_controller_test.rb
    Views:      app/views/credit_cards/debit.html.erb [...etc]
    Helper:     app/helpers/credit_cards_helper.rb
    

    A controller is simply a class that is defined to inherit from ApplicationController.

    It's inside this class that you'll define methods that will become the actions for this controller.

  • 6

    Rails uses sqlite3 as the default database, but you can generate a new rails application with a database of your choice. Just add the -d option followed by the name of the database.

    $ rails new MyApp -T -d postgresql
    

    This is a (non-exhaustive) list of available database options:

    • mysql
    • oracle
    • postgresql
    • sqlite3
    • frontbase
    • ibm_db
    • sqlserver
    • jdbcmysql
    • jdbcsqlite3
    • jdbcpostgresql
    • jdbc

    The -T command indicated the testing suite of RSpec. We can write our automation test cases using this tool

Please consider making a request to improve this example.

Remarks

logo

Ruby on Rails (RoR), or Rails, is an open-source popular web application framework. Rails uses Ruby, HTML, CSS, and JavaScript to create a web application that runs on a web server. Rails uses the model-view-controller (MVC) pattern and provides a fullstack of libraries from the database all the way to the view.

Versions

VersionRelease Date
5.02016-06-30
4.22014-12-19
4.12014-04-08
4.02013-06-25
3.22012-01-20
3.12011-08-31
3.02010-08-29
2.32009-03-16
2.02007-12-07
1.22007-01-19
1.12006-03-28
1.02005-12-13
Still have a question about Getting started with Ruby on Rails? Ask Question

Topic Outline