Tuesday, June 23, 2009

Example project for rails

Open a console in that folder and type:

rails WebAlbum --database=mysql

Now make WebAlbum the current folder in your console. All further console work is going to take place exclusively inside WebAlbum.

Although we're using the latest version of Rails, it is a fast moving target. Newer versions may break our code, so we want to keep a local copy of Rails right in our application. We do that with the freeze command:
rake rails:freeze:gems
Now our application will use the Rails code (in the vendor folder), and not the Rails code in our Ruby distribution. Next we'll check that the database settings are working. Type:
rake db:migrate

The db folder will now contain two files:
db/
development.mysql
schema.rb
So far, so good. The next step is to generate the scaffolding code for our users. We'll start off with just a couple of fields, we'll add additional fields in a moment. Type:
ruby script/generate scaffold user name:string motto:string
Rails also created a database migrate class - db/migrate/001_create_users.rb. This class inherits from ActiveRecord::Migration which provides methods to change the database structure. We need a few more fields than we defined in the scaffold script, so open this file, and modify it to look like:

class CreateUsers <>
def self.up
create_table :users do |t|
t.string :name, :limit => 40, :null => false
t.string :motto, :limit => 80
t.string :salted_password, :limit => 40, :null => false
t.string :salt, :limit => 40, :null => false
t.timestamps
end
add_index :users, [:name], :name => :users_name_index, :unique => true
end

def self.down
remove_index :users, :name => :users_name_index
drop_table :users
end
end
Right now we're at version 0, so let's update the database. Type:
rake db:migrate
If you want to check that you can revert to the previous version, then add the version number to the rake command:
rake db:migrate VERSION=0
Now let's take a look at what we've got. Start the web application, by typing:
ruby script/server
Start up your favourite browser, and type http://localhost:3000/ in the location bar. When the Rails home page appears, click on the “About your application's environment” link,

Now shut down the server (Ctrl+C), we've got some more coding to do.

Each of our users will have a password. We don't want to keep that password as plain text, because, well, someone might steal it. I'm not talking about stealing a single user's password, I'm talking about the whole database. The 'salt and shaker' process used here is probably a good enough solution to a potentially big problem. Ranting aside, let's look at the code. This is the modified app/models/user.rb file:
require 'digest/sha1'

class User <>
attr_readonly :name
attr_reader :password
attr_accessor :password_confirmation

validates_presence_of :name
validates_length_of :name, :within => 1..40
validates_uniqueness_of :name

validates_length_of :motto, :within => 0..80

validates_confirmation_of :password

def password=(pword)
@password = pword.strip
return if @password.length <> 40
self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{rand.to_s}--")
self.salted_password = User.encrypted_password(self.salt, @password)
end

def validate_on_create
if @password.length <> 40
errors.add(:password, "must have between 6 and 40 characters")
end
end

def self.authenticate(name, password)
user = find(:first, :conditions => [ "name = ?", name ])
if user
if user.salted_password != encrypted_password(user.salt, password)
user = nil
end
end
user
end

private
def self.encrypted_password(salt, password)
Digest::SHA1.hexdigest("--#{salt}--#{password}--")
end
end
Now we'll need to make some modifications to the ERB template for a new user, in the file app/views/user/new.html.erb:

<%= error_messages_for :user %>

<% form_for(@user) do |f| %>
<div>
<%= f.label :name, 'Name:' %>
<%= f.text_field :name, :size =>40, :maxlength => 40 %>
div>

<div>
<%= f.label :motto, 'Motto:' %>
<%= f.text_field :motto, :size => 40, :maxlength => 80 %>
div>


<%= f.label :password, 'Password:' %>
<%= f.password_field :password, :size => 40, :maxlength => 40 %>



<%= f.label :password_confirmation, 'Confirm password:' %>
<%= f.password_field :password_confirmation, :size => 40, :maxlength => 40 %>


<div>
<%= f.submit "Create" %>
div>
<% end %>

<%= link_to 'Back', users_path %>
Now we're ready to add a user. Fire up the server again:

ruby script/server

Open the browser and go to the location http://localhost:3000/users/new. You should now see something.
And then yuo can modify this yuor own.


Next I explain how to send email using rails.
Bye everyone
:)

Thursday, June 18, 2009

how to run webserver in rails

I explain step by step.
first open your terminal.

1.Then enter the following command on your terminal.

rails
(eg:- rails firstproject)

2.then create more folders.

3.then go to the your project folder.
cd
(eg:- cd firstprogramme)

4.then run the following command

ruby script/server

5.then open your browser insert the following address in your address bar.

http://localhost:3000

6.you will see your web server run

sinmple introduction for rails

What is Rails

  • An extremely productive web-application framework.
  • Written in Ruby by David Heinemeier Hansson.
  • You could develop a web application at least ten times faster with Rails than you could with a typical Java framework.
  • An open source Ruby framework for developing database-backed web applications.
  • Your code and database schema are the configuration!
  • No compilation phase required.

Full Stack Framework

  • Includes everything needed to create a database-driven web application using the Model-View-Controller pattern.
  • Being a full-stack framework means that all layers are built to work seamlessly together Less Code.
  • Requires fewer total lines of code than other frameworks spend setting up their XML configuration files.

Convention over Configuration

  • Rails shuns configuration files in favor of conventions, reflection and dynamic run-time extensions. Your application code and your running database already contain everything that Rails needs to know!
Rails Strengths

  • Easy to get started with, easy to get something done enough to launch
  • Easy to re-factor and replace weak code with stronger
  • Development speed and philosophy lend themselves to agile development methodologies.
Weakness

  • Some confusion about scale and the "right" architecture for Rails apps
    • Lots of people using Rails for the "wrong" thing.
  • Can be hard to debug memory and performance issues
  • Speed of new features/fixes/best practices can make it hard to keep up, especially in a behemoth like AOL.
  • Lots of FUD in other communities about Rails' weaknesses, can be hard to separate myth from reality and past from present.
When to use Rails,
  • You need the power of MVC, but want to Get Things Done quickly
  • You want unit tests, a sane and flexible environment and help to do the Right Thing
  • You like being a part of a very active and passionate development community
  • You can start with a clean slate database-wise
But When not use
  • You don't need an MVC or framework, especially if you don't need a database
  • You need to get lots of data from lots of different sources for each request
  • You're constrained by a legacy database and can't change anything

To develop a web application using Ruby on Rails Framework, install the following software:

  • Ruby
  • The Rails framework
  • A Web Server
  • A Database System

Simple Introduction For Ruby

Ruby is

  • A High Level Programming Language
  • Interpreted like Perl, Python, Tcl/TK.
  • Object-Oriented Like Smalltalk, Eiffel, Ada, Java.
  • Originated in Japan and Rapidly Gaining Mindshare in US and Europe.

Ruby is the successful combination of:

  • Smalltalk's conceptual elegance,
  • Python's ease of use and learning, and
  • Perl's pragmatism
Ruby is an object-oriented, interpreted programming language. Interpreted programming.languages are read line by line instead of by compiling the code into an executable that is unreadable to a human being (but is much quicker to process by a computer.) Other inter-preted languages include JavaScript and BASIC. If you open up your web browser on a page that uses JavaScript, you can read the source code by using your browser’s window.Try opening up an application like your system’s Calculator in a text editor. All you see is
some garbled text because the Calculator application was written in a compiled language.
Ruby started to gain popularity in 2001 with the commencement of Ryan Leavengood’s work on RubyGems, which is an easy way to package and distribute applications and libraries. RubyGems’ development stalled for several years because Leavengood left the project at version 0.4.0. In 2003, a group of developers reincarnated the RubyGems project and released a totally rewritten version under the same name. While they didn’t share any of the same code, they shared the same principle: simple software distribution for Ruby.


How to install rubby 1.9 and Rails 2.3.2 for ubuntu 8.10

I explain step by step how to install Ruby 1.9 and rails 2.3.2
1.first open your command prompt.
2 .Then install foolowing package in your computer

sudo apt-get -y install libc6-dev libssl-dev libmysql++-dev libsqlite3-dev make build-essential libssl-dev libreadline5-dev zlib1g-dev
Install RUby 1.9
Enter following comand step by step

cd ~
mkdir src
cd src/
wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.1-p0.tar.gz
tar xzvf ruby-1.9.1-p0.tar.gz
cd ruby-1.9.1-p0/
wget http://redmine.ruby-lang.org/attachments/download/237
patch -p1 < 237
./configure --prefix=/usr/local --with-openssl-dir=/usr --with-readline-dir=/usr --with-zlib-dir=/usr
sudo make && sudo make install

3.Then check your ruby version following command
ruby -v


4. The output something may be
ruby 1.9.1p0 (2009-01-30 revision 21907) [i686-linux]

After enter the following code output should be "Happpy New Ruby "

ruby -ropenssl -rzlib -rreadline -e "puts 'Happy new Ruby'"

Ruby 1.9 bundles rubygems so there is no need to install it.

5. Then install mysql-ruby adapter

cd ~/src
wget http://rubyforge.org/frs/download.php/51087/mysql-ruby-2.8.1.tar.gz
tar xzvf mysql-ruby-2.8.1.tar.gz
cd mysql-ruby-2.8.1
sudo ruby extconf.rb
make
sudo make install
6. Then install rails2.3.2 and other stuff for you computer
sudo gem install rails rake rack sqlite3-ruby

7. Then check your machine all packages are installed. so you can use following command

gem list

8.The output something like this

actionmailer (2.3.2)
actionpack (2.3.2)
activerecord (2.3.2)
activeresource (2.3.2)
activesupport (2.3.2)
rack (0.9.1)
rails (2.3.2)
rake (0.8.4)
sqlite3-ruby (1.2.4)

Now you can use rails and ruby for develop your programme
And I next explain what is rails and ruby.