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.

Padrino Ruby Web Framework


Preface

Padrino is a ruby framework built upon the Sinatra web library. Sinatra is a DSL for creating simple web applications in Ruby. Padrino was created to make it fun and easy to code more advanced web applications while still adhering to the spirit that makes Sinatra great!

Installation

In order to develop a Padrino application, we must have a few things installed. First, we must obviously have ruby and rubygems installed. Next, we must install the padrino framework gems:

$ gem install padrino

Sample Application

For sample application, we will use the ActiveRecord ORM AND the erb templating language.
With that in mind,let us generate our new project:

$ padrino g project cast - erb -d activerecord
      create  
      create  .gitignore
      create  config.ru
      create  config/apps.rb

=================================================================
cast is ready for development!
=================================================================
$ cd ./cast
$ bundle
=================================================================

This command will generate our basic Padrino project and the print out a nice report of the files generated.

Padrino is a Ruby Framework

you can even specify the RDBMS adapter to use with the -a option followed by the name of the adapter: $ padrino g project cast - erb -d activerecord -a mysql we need to do now is cd into our brand new application. $ cd sample_blog Let’s start off by generating the model into our app directory. $ padrino g model list  name:string price:integer        apply  orms/activerecord       create  models/list.rb       create  db/migrate/001_create_lists.rb Go ahead and migrate the database now. $ padrino rake ar:migrate => Executing Rake ar:migrate ...   DEBUG -   (0.0ms)  SELECT "schema_migrations"."version" FROM "schema_migrations"    INFO -  Migrating to CreateLists (1)   DEBUG -   (0.0ms)  select sqlite_version(*)   DEBUG -   (0.0ms)  begin transaction ==  CreateLists: migrating ==================================================== -- create_table(:lists)   DEBUG -   (0.3ms)  CREATE TABLE "lists" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "price" text, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)    -> 0.0038s ==  CreateLists: migrated (0.0038s) ===========================================   DEBUG -   (0.1ms)  INSERT INTO "schema_migrations" ("version") VALUES ('1')   DEBUG -   (230.7ms)  commit transaction   DEBUG -   (0.1ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" This creates the post model. Next, let’s create the controller to allow the basic viewing functionality. $ padrino g controller list get:new get:show       create  app/controllers/list.rb       create  app/helpers/list_helper.rb       create  app/views/list Now it's time to create index.html.rb and new.html.rb file and add some custom code to place order. new.html.rb <h2>Pleae fill below order</h2> <%- form_tag '/list', :method => 'get' do %>  <p>    Name:    <%= text_field_tag "name" %>  </p>  <p>    Price:    <%= text_field_tag "price" %>  </p>  <p>    <%= submit_tag 'Place Order' %>  </p> <% end %> index.html.rb <h1>Below are the orders</h1> <table>     <tr>         <td>Sl. No.</td>         <td>Name</td>         <td>Price</td>     </tr>     <% sl = 0 %>     <% @orders.each do |order| %>         <tr>             <td><%= sl+=1 %> </td>             <td><%= order.name %></td>             <td><%= order.price%></td>         </tr>     <%end%> </table> <%= link_to "Place new order", url_for(:list, :new) %> Now let's add ActiveRecord create method to save new entry to database and get all recored from db. controller/list.rb get :index do     # raise params.inspect     List.create(params)     @orders = List.all     render 'list/index'   end   get :new do     render 'list/new'   end Now it's time to start Padrino server. $ padrino start => Padrino/0.11.3 has taken the stage development at http://127.0.0.1:3000 [2013-08-03 01:12:36] INFO  WEBrick 1.3.1 [2013-08-03 01:12:36] INFO  ruby 2.0.0 (2013-05-14) [x86_64-linux] [2013-08-03 01:12:36] INFO  WEBrick::HTTPServer#start: pid=7392 port=3000 Visit http://localhost:3000 in browser. we will get default sinatra error page. error As per ERROR, root of application is missing here. We can have the root of our site routed at "root(/)", Need to add root routes in app.rb and respective view. app/app.rb get '/' do   render '/index' end and app/view/index.html.rb <h1>Want to place order?</h1> <%= link_to "Place order from here", url_for(:list, :new) %> That's it. Code is available at Padrino Demo Application.