RSS

Tag Archives: rails

Learning Ruby on rails

What is Rails?

Rails is a web application development framework written in the Ruby language. It is designed to make programming web applications easier by making assumptions about what every developer needs to get started. It allows you to write less code while accomplishing more than many other languages and frameworks. Experienced Rails developers also report that it makes web application development more fun.

Rails is opinionated software. It makes the assumption that there is the “best” way to do things, and it’s designed to encourage that way – and in some cases to discourage alternatives. If you learn “The Rails Way” you’ll probably discover a tremendous increase in productivity. If you persist in bringing old habits from other languages to your Rails development, and trying to use patterns you learned elsewhere, you may have a less happy experience.

The Rails philosophy includes two major guiding principles:

  • DRY – “Don’t Repeat Yourself” – suggests that writing the same code over and over again is a bad thing.
  • Convention Over Configuration – means that Rails makes assumptions about what you want to do and how you’re going to do it, rather than requiring you to specify every little thing through endless configuration files.

Creating a New Rails Project

The best way to use this guide is to follow each step as it happens, no code or step needed to make this example application has been left out, so you can literally follow along step by step. You can get the complete code here.

Before create a project plz be sure u have successfully installed Rails.

To verify that you have everything installed correctly, you should be able to run the following:

$ rails --version

If it says something like “Rails 4.0.0”, you are ready to continue.

 

To use this generator, open a terminal, navigate to a directory where you have rights to create files, and type:

$ rails new <your_application_name>

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

The project directory has a number of auto-generated files and folders that make up the structure of a Rails application. Most of the work in this tutorial will happen in the app/ folder, but here’s a basic rundown on the function of each of the files and folders that Rails created by default:

File/Folder Purpose
app/ Contains the controllers, models, views, helpers, mailers and assets for your application. You’ll focus on this folder for the remainder of this guide.
bin/ Contains the rails script that starts your app and can contain other scripts you use to deploy or run your application.
config/ Configure your application’s runtime rules, routes, database, and more. This is covered in more detail in Configuring Rails Applications
config.ru Rack configuration for Rack based servers used to start the application.
db/ Contains your current database schema, as well as the database migrations.
Gemfile
Gemfile.lock
These files allow you to specify what gem dependencies are needed for your Rails application. These files are used by the Bundler gem. For more information about Bundler, see the Bundler website
lib/ Extended modules for your application.
log/ Application log files.
public/ The only folder seen to the world as-is. Contains the static files and compiled assets.
Rakefile This file locates and loads tasks that can be run from the command line. The task definitions are defined throughout the components of Rails. Rather than changing Rakefile, you should add your own tasks by adding files to the lib/tasks directory of your application.
README.rdoc This 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, and so on.
test/ Unit tests, fixtures, and other test apparatus. These are covered in Testing Rails Applications
tmp/ Temporary files (like cache, pid and session files)
vendor/ A place for all third-party code. In a typical Rails application, this includes Ruby Gems and the Rails source code (if you optionally install it into your project).

Starting up the Web Server

You actually have a functional Rails application already. To see it, you need to start a web server on your development machine. You can do this by running:

$ rails server

This will fire up WEBrick, a webserver built into Ruby by default. To see your application in action, open a browser window and navigate to http://localhost:3000. You should see the Rails default information page:

Welcome Aboard screenshot

Say “Hello”, Rails

To get Rails saying “Hello”, you need to create at minimum a controller and a view.

To create a new controller, you will need to run the “controller” generator and tell it you want a controller called “welcome” with an action called “index”, just like this:

$ rails generate controller welcome index

Rails will create several files and a route for you.

create  app/controllers/welcome_controller.rb
 route  get "welcome/index"
invoke  erb
create    app/views/welcome
create    app/views/welcome/index.html.erb
invoke  test_unit
create    test/controllers/welcome_controller_test.rb
invoke  helper
create    app/helpers/welcome_helper.rb
invoke    test_unit
create      test/helpers/welcome_helper_test.rb
invoke  assets
invoke    coffee
create      app/assets/javascripts/welcome.js.coffee
invoke    scss
create      app/assets/stylesheets/welcome.css.scss

Most important of these are of course the controller, located at app/controllers/welcome_controller.rb and the view, located at app/views/welcome/index.html.erb.

Open the app/views/welcome/index.html.erb file in your text editor. Delete all of the existing code in the file, and replace it with the following single line of code:

<h1>Hello, Rails!</h1>

Setting the Application Home Page

Now that we have made the controller and view, we need to tell Rails when we want Hello Rails! to show up. In our case, we want it to show up when we navigate to the root URL of our site, http://localhost:3000. At the moment, “Welcome Aboard” is occupying that spot.

Next, you have to tell Rails where your actual home page is located.

Open the file config/routes.rb in your editor.

Blog::Application.routes.draw do
  get "welcome/index"
  # The priority is based upon order of creation:
  # first created -> highest priority.
  # ...
  # You can have the root of your site routed with "root"
  # root to: "welcome#index"

This is your application’s routing file which holds entries in a special DSL (domain-specific language) that tells Rails how to connect incoming requests to controllers and actions. This file contains many sample routes on commented lines, and one of them actually shows you how to connect the root of your site to a specific controller and action. Find the line beginning with root :to and uncomment it. It should look something like the following:

root to: "welcome#index"

The root to: "welcome#index" tells Rails to map requests to the root of the application to the welcome controller’s index action and get "welcome/index" tells Rails to map requests to http://localhost:3000/welcome/index to the welcome controller’s index action. This was created earlier when you ran the controller generator (rails generate controller welcome index).

If you navigate to http://localhost:3000 in your browser, you’ll see the Hello, Rails! message you put into app/views/welcome/index.html.erb, indicating that this new route is indeed going to WelcomeController‘s index action and is rendering the view correctly.

For more information about routing, refer to Rails Routing from the Outside In.

@ All contents @http://guides.rubyonrails.org/getting_started.html

Advertisements
 
Leave a comment

Posted by on August 6, 2013 in Rails

 

Tags:

Nginx + Passenger to serve rails apps in different sub URIs

Let you have 2 directories and wants to configure, make sure u have installed passenger already.

server {
  listen 80;
  server_name 127.0.0.1;

  location /first/ {
    root /home/hector/webapps/first/public;
    passenger_base_uri /first/;

    proxy_pass http://127.0.0.1:3000/;
    proxy_set_header Host $host;
  }
  location /second/ {
    root /home/hector/webapps/second/public;
    passenger_base_uri /second/;

    proxy_pass http://127.0.0.1:3001/;
    proxy_set_header Host $host;
  } 

}
 
Leave a comment

Posted by on August 6, 2013 in Rails

 

Tags:

Running Ruby on Rails on Nginx

Follow below steps to install Ruby on rails successfully.

Ruby

1
2
3
4
5
6
export PATH="${PATH}:/var/lib/gems/1.8/bin/"
echo 'export PATH="${PATH}:/var/lib/gems/1.8/bin/"' >> /etc/bash.bashrc
aptitude install ruby rubygems vim-ruby ruby-dev libzlib-ruby \
libyaml-ruby libreadline-ruby libncurses-ruby rdoc ri libcurses-ruby \
libruby libruby-extras libfcgi-ruby build-essential libopenssl-ruby \
libdbm-ruby libdbi-ruby libxml-ruby libxml2-dev

Rails

Simple:

1
gem install -v=2.3.5 rails

gem install rails should have worked but 2.3.6 – 2.3.8 (current at the of writing) have issues with mongrel

Or, if you want to live on the edge and try the latest:

1
gem install rails --pre

Or with RVM

RVM is a command line tool which allows us to easily install, manage and work with multiple ruby environments from interpreters to sets of gems. See installation instructions and a full tutorial on that.

App

My new app is called myapp.example.com

1
2
3
cd /var/www
rails new myapp.example.com
cd myapp.example.com

Have a look around and see what you can find .

Thin

Thin will be the Ruby server

1
2
3
4
gem install thin
thin install
/usr/sbin/update-rc.d -f thin defaults
thin config -C /etc/thin/myapp.example.com -c /var/www/myapp.example.com --servers 3 -e development # or: -e production for caching, etc

Or Mongrels

If you don’t like Thin..

1
2
3
4
5
aptitude install mongrel mongrel-cluster
mongrel_rails cluster::configure -e development -p 3000 -N 3 -c /var/www/myapp.example.com -a 127.0.0.1 # or: -e production for caching, etc
mkdir /etc/mongrel_cluster
sudo ln -nfs /var/www/myapp.example.com/config/mongrel_cluster.yml /etc/mongrel_cluster/myapp.example.com.yml
#sudo ln -nfs /var/www/myapp.example.com/config/mongrel_cluster.yml /etc/mongrel-cluster/sites-enabled/myapp.example.com.yml

Nginx

Nginx will be the Web server, proxing ruby requests to thin, running on ports 3000-3002 If you haven’t installed it yet, do

1
aptitude install nginx

Now that you have Nginx, create a vhost. Edit /etc/nginx/sites-available/myapp.example.com and type:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
upstream myapp {
  server 127.0.0.1:3000;
  server 127.0.0.1:3001;
  server 127.0.0.1:3002;
}
server {
  listen   80;
  server_name .example.com;

  access_log /var/www/myapp.example.com/log/access.log;
  error_log  /var/www/myapp.example.com/log/error.log;
  root     /var/www/myapp.example.com;
  index    index.html;

  location / {
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  Host $http_host;
    proxy_redirect  off;
    try_files /system/maintenance.html $uri $uri/index.html $uri.html @ruby;
  }

  location @ruby {
    proxy_pass http://myapp;
  }
}

Then make it available to the public

1
ln -nfs /etc/nginx/sites-available/myapp.example.com /etc/nginx/sites-enabled/myapp.example.com

Databases

First set up SQLite

1
2
aptitude install -y libdbd-sqlite3-ruby sqlite3 libsqlite3-dev libsqlite3-ruby
gem install sqlite3-ruby

MySQL?

Optionally if you want to use MySQL install the following (but do sqlite anyway):

1
2
aptitude install -y libmysqlclient-dev
gem install mysql

Then change your /var/www/myapp.example.com/config/database.yml and make it say something along the lines of

1
2
3
4
5
6
development:
  adapter: mysql
  host: localhost
  database: myapp
  username: myapp
  password: xxxxxxx

Note! database.yml doesn’t accept tabs. If you are in vim, you might need to do:

1
2
3
:set expandtab
#:set tabstop=4 # how many spaces should tabs be replaced withs
:retab

Also, make you app require the mysql gem by adding the following to ./Gemfile

1
gem 'mysql', '2.8.1'

I am assuming you already have a mysql-server running. If not, you also need to aptitude install mysql-server first.

Nice gems

  • gem install uuidtool
  • gem install ruby-debug
  • gem install ruby-graphviz
  • gem install json
  • gem install activemerchant

Bring App Live

Let’s restart our daemons to see if it worked:

For Thin:

1
/etc/init.d/thin restart && /etc/init.d/nginx reload; tail -f log/*.log

For Mongrel:

1
mongrel_cluster_ctl restart && /etc/init.d/nginx reload; tail -f log/*.log

Add this line to above the 2 default routes in config/routes.rb:

1
2
3
4
# Rails 2
map.root :controller => "home"
# Rails 3
#root :controller => "home#index"

Create a home controller, add a view for it, and remove the ‘Welcome aboard’ html.

1
2
3
script/generate controller home index
rm public/index.html
echo '<h1>HeyO!</h1>' > app/views/home/index.erb

If you don’t get any errors, point your browser to the Vhost you created, and you should see a pleasant surprise.

 

 
Leave a comment

Posted by on August 6, 2013 in Rails

 

Tags: