Translate

Tuesday, January 8, 2013

First Application in Rails

            Hello RoR lovers :D ,

    In this post I will make first application in Rails after setting development environment.
So lets start, depending on platform we will go to terminal in case of Linux and OS X, or command prompt for Windows. Virtually all Rails application start the same way,with rails command. This handy program creates a skeleton Rails application in a directory of your choice. To get started, make a directory for your Rails projects and then run the rails command to make the first application:

       $ mkdir rails_projects
       $ cd rails_projects
       $ rails new first_app
              create  
              create README.rdoc
              create Rakefile
              create config.ru
              create .gitignore
              create Gemfile
              create app
              create app/assets/images/rails.png
              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/mailers
              create app/models
              create app/views/layouts/application.html.erb
              create app/mailers/.gitkeep
              create app/models/.gitkeep
              create config
              create config/routes.rb
              create config/application.rb
              create config/environment.rb
               .
               .
               .
              create vendor/plugins
              create vendor/plugins/.gitkeep
                 run bundle install
        Fetching source index for https://rubygems.org/
        .
        .
        .
       Your bundle is complete! Use `bundle show [gemname]` to see where a bundled
       gem is installed.


In Windows after installation of railsinstaller we have a folder for our applications in C:>Sites> , here will be placed my projects in Rails on Windows.
 
    As seen at the end of making first application,running rails automatically runs the bundle install command after the file creation is done. If that step doesn't work right now,don't worry; follow the steps in section bellow under Bundler and should be able to get it work.
Notice how many files and directories the rails command creates. This standard directory and file structure (look bellow) is one of the many advantages of Rails; it immediately gets you from zero to a functional (if minimal) application. Moreover,since the structure is common to all Rails apps, you can immediately get your bearings when looking at someone else's code. A summary of the default Rails files appears in table bellow; we'll learn about most of these files and directories in some of the next posts that will come soon.

             Table: A summary of the default Rails directory structure:

File/Directory                                                                     Purpose
-----------------------------------------------------------------------------------------------
app/                                       Core application(app) code, including models,views,controllers,and          
                                              helpers
app/assets                              Application assets such as cascading style sheets (CSS), JavaScript files,
                                              and images
config/                                   Application configuration

db/                                         Database files

doc/                                       Documentation for the application

lib/                                         Library modules

lib/assets                                Library assets such as cascading style sheets (CSS), JavaScript files,and
                                              images
log/                                        Application log files

public/                                   Data accessible to the public (e.g., web browser), such as error pages

script/rails                              A script for generating code, opening console sessions, or starting a local
                                              server
test/                                        Application tests

tmp/                                       Temporary files

vendor/                                  Third-party code such as plugins and gems

vendor/assets                         Third-party assets such as cascading style sheets (CSS), JavaScript files,
                                              and images
README.doc                      A brief description of the application

Rakefile                                 Utility tasks available via the rake command

Gemfile                                 Gem requirements for this app

Gemfile.lock                         A list of gems used to ensure that all copies of the app use the same gem
                                              versions
config.ru                               A configuration file for Rack middleware

.gitignore                              Patterns for files that should be ignored by Git



        Bundler

    After creating a new Rails application, the next step is to use Bundler to install and include the gems needed by the app. As noted briefly before, Bundler is run automatically (via bundle install) by the rails  command, but in this section we'll make some changes to the default application gems and run Bundler again. This involves opening the Gemfile with your favorite text editor:

       $ cd first app/
       $ aptana Gemfile                    # here you put other editor such us sublime,or open in other way

The result should be something like this:


                   The default Gemfile in the first_app directory

source 'https://rubygems.org'

gem 'rails', '3.2.10'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'sqlite3'


# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.2'

  gem 'uglifier', '>= 1.2.3'
end

gem 'jquery-rails'

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the web server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'

The code in this file is Ruby, but don't worry at this point about the syntax.  In first post I recommend a fantastic book for learning Ruby, Programming Ruby 1.9 or older version 1.8,also known as "Pick axe". So it will be more then great to start learning Ruby from this book,also there are a lot other resources that is also very useful.
    Many of these lines are commented with the hash symbol # ; they are there to show you some commonly needed gems and to give examples of the Bundler syntax. For now, we won't need any gems other then the defaults: Rails itself, some gems related to the assets pipeline (look bellow The asset pipeline) the gem for the JQuery JavaScript library, and the gem for the Ruby interface to the SQLite database.
     Unless you specify a version number to the gem command, Bundler will automatically install the latest version of the gem. Unfortunately, gem updates often cause minor potentially confusing breakage, so in this tutorial we'll include explicit version numbers known to work, as seen bellow (which also omits the commented-out lines from up before )
               A Gemfile with an explicit version of each Ruby gem
source 'https://rubygems.org'

gem 'rails', '3.2.10'

group :development do
  gem 'sqlite3', '1.3.5'
end


# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '3.2.5'
  gem 'coffee-rails', '3.2.2'

  gem 'uglifier', '1.2.3'
end

gem 'jquery-rails', '2.0.2'
     So here we change the line for JQuery, the default JavaScript library used for Rails,from
        gem 'jquery-rails'
to
        gem 'jquery-rails', '2.0.2'
We also changed
        gem 'sqlite3'
to 
group :development do
  gem 'sqlite3', '1.3.5'
end
which forces Bundler to install version 1.3.5 of the sqlite3 gem.
We also change a few other lines, converting
    group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.2'
  gem 'uglifier', '>= 1.2.3'
end
to
    group :assets do
  gem 'sass-rails',   '3.2.5'
  gem 'coffee-rails', '3.2.2'
  gem 'uglifier', '1.2.3'
end
The syntax
    gem 'uglifier', '>= 1.2.3'
installs the latest version of uglifier gem (which handels file compression for the asset pipeline) as long as it's greater than version 1.2.3 - even if it's,say,version 7.2. Meanwhile, the code
   gem 'coffee-rails', '~> 3.2.2'
installs the gem coffee-rails(also needed by the asset pipeline) as long as it's lower than version 3.3.
In other words, the, >= notation always performs upgrades,whereas the ~> 3.2.2 notation only performs upgrades to minor releases (e.g., from 3.1.1 to 3.1.2), but not to major point release (e.g., from 3.1 to 3.2). Unfortunately, experience shows that even minor point releases often break things,we'll err on the side of caution by including exact version numbers for virtually all gems. (The only exception is gems that are in release candidate or beta stage as of this writing; for those gems, we'll use ~> so that the final versions will be loaded once they're done.)
Once you'assembled the proper Gemfile, install the gems using bundle install :
$ bundle install
Fetching source index for https://rubygems.org/
.
.
.
If you're running OS X and you get an error about missing Ruby header files (e.g. ruby.h ) at this point, you may need to install Xcode. These are developer tools that came with your OS X installation disk, but to avoid the full installation I recommend the much smaller Command Line Tools for Xcode. If you get a libxslt error when installing the Nokogiri gem, try reinstalling Ruby:
$ rvm reinstall 1.9.3
$ bundle install
The bundle install command might take a few moments, but when it's done our application will be ready to run.  Note: This setup is fine for the first app, but it isn't ideal. In next posts I will try to do more concrete app as an examples :) .
    
        Rails server

    So first we did rails new [appname] after that bundle install commands, and now we are ready for rails server command.  We already have an application we can run - but how? Happily, Rails comes with a command-line program, or script, that runs a local web server, visible only from your development machine.
$ rails server
=> Booting WEBrick
=> Rails application starting on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
How does it work, we start the server,in this case is WEBrick server,and this tells us that the application is running on port number 3000 at the address 0 . 0 . 0 . 0 . This address tells the computer to listen on every available IP address configured on that specific machine; in particular, we can view the application using the special address 127 . 0 . 0 . 1 , which is also known as localhost. 
http://localhost:3000/

So this is all for this time,all questions,remarks,suggestions, please be free to ask, I will be more than happy to answer :) .

No comments:

Post a Comment