Creating Your First Ruby on Rails Application


Visit blogadda.com to discover Indian blogs

Ruby on Rails First application

Let’s create a basic Ruby on Rails application, without worrying too much about Ruby on Rails basics knowledge. Make sure you have install everything like Ruby, Rails and Mysql as per your Operating System(I am using ubuntu (Linux) you can also install rails on your Windows or Mac machine as well). Now we are going to create our rails application. To begin, open a terminal (Console, command Prompt) and navigate to where you want to create application and type the command:

       $ rails new demo
In above example we used ’demo’ as the name of the application which we will build. As soon as you hit enter, you will see the scripts flow in your terminal creating a bunch of files, These are basic file structure of a Ruby on Rails application.
      ubuntu@ubuntu-Lenovo-G580:~/workspace/RailsApp$ rails new demo -d mysql
      create  
      create  README.rdoc
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
      create  app
      create  app/assets/javascripts/application.js
      create  app/assets/stylesheets/application.css
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      create  app/views/layouts/application.html.erb
      create  app/assets/images/.keep
      create  app/mailers/.keep
      create  app/models/.keep
      create  app/controllers/concerns/.keep
      create  app/models/concerns/.keep

Now switch to the directory to our application using below command:

       $ cd demo

Now it’s time to take a close look to all the sub-directories and the files inside it to get an idea where the code lives. Once you are done,  Test if your rails application is working by using the command:

      $ rails s

Go ahead and open http://localhost:3000/ in your favorite browser. If you see a page without any Error, then your first rails application works fine.

To start with Mysql integration, we needed to create controller, model and views for our demo application,  which allows us to post your Name, Email and description to Mysql database.

 $ rails g scaffold user name:string email:string desc:text

Rails scaffolding is a quick way to generate some of the major pieces of an application. If you want to create the model, views and controllers for a new resource in a single operation, scaffolding is the tool for this job.

Migrations files are a convenient way to perform CRUD (create, read, update and delete) operation on the database over time in a consistent and easy way. If you haven’t created your database yet we can do it by a command:

 $ rake db:create
At this point, We need to let Rails know about the database configuration information to connect with Mysql. We do this in the file *database.yml*, available in the *app_root_dir/config* subdirectory of Rails Application we created. This file has live configuration sections for MySQL databases. In each of the sections you use, you need to change the host, port, database,
username and password lines to reflect the permissions on the databases you’ve created.
  •       adapter: mysql2
  •       host: <host>
  •       port: <port>
  •       database: <database name>
  •       username: <user_name>
  •       password: ‘<password>’

Rails provides a set of Rake tasks to run certain sets of migrations. As we have recently created a new model for User, a table must be created in our database and requires that we upgrade the database using this command:

$ rake db:migrate

Now lets checkout what we have done so far:

$ rails s

As before, open http://localhost:3000/users in your favorite browser to see your application.

$ rails g scaffold user name:string email:string desc:text

Rails scaffolding is a quick way to generate some of the major pieces of an application. If you want to create the model, views and controllers for a new resource in a single operation, scaffolding is the tool for this job.

Migrations files are a convenient way to perform CRUD (create, read, update and delete) operation on the database over time in a consistent and easy way. If you haven’t created your database yet we can do it by a command:

$ rake db:create
At this point, We need to let Rails know about the database configuration information to connect with Mysql. We do this in the file *database.yml*, available in the *app_root_dir/config* subdirectory of Rails Application we created. This file has live configuration sections for MySQL databases. In each of the sections you use, you need to change the host, port, database,
username and password lines to reflect the permissions on the databases you’ve created.
  •       adapter: mysql2
  •       host: <host>
  •       port: <port>
  •       database: <database name>
  •       username: <user_name>
  •       password: ‘<password>’

Rails provides a set of Rake tasks to run certain sets of migrations. As we have recently created a new model for User, a table must be created in our database and requires that we upgrade the database using this command:

$ rake db:migrate

Now lets checkout what we have done so far:

$ rails s

As before, open http://localhost:3000/users in your favorite browser to see your application.

Advertisements

3 thoughts on “Creating Your First Ruby on Rails Application

  1. Ruby on Rails is an open source full-stack web application framework written in Ruby. It follows the popular MVC framework model and is known for its “convention over configuration” approach to application development. Ruby on Rails development is a full-stack web framework optimized for programmer happiness and sustainable productivity.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s