+Install Ruby (gem update rails)
+Install Rails (gem install rails)
+Install sqlite (download precompiled binaries for windows sqlite-shell-win32-x86-version.zip)
+Make your application
rails new path/to/your/new/application
cd path/to/your/new/application
bin/rails server
You're running Rub on Rails! Follow the instructions on http://localhost:3000
+To check ruby version
ruby -v
+To check rails version
rails -v
+Installign sqlite3
gem install sqlite3
+To check sqlite version
sqlite3 --version
+create sqlite database
sqlite dbname.db
.help to display help files
.quit to exit sqlite
+create controller
cd bin
rails generate controller welcome index
(the above command will create controller, view and add route for welcome controller with index method)
+rails' route
get 'welcome/index'
root 'welcome#index'
(this will map http://localhost:3000/welcome/index to welcome#index (index method of welcome controller)
+To create resource (RESTful actions)
add the below line to routes.rb
resources :articles
use bin/rake routes to view all defined routes for all the standard RESTful actions.
You can create, read, update and destroy items for a resource and these operations are reffered to as CRUD operations.
rails generate controller articles
(Note: There are public, private and protected methods in Ruby, but only public methods can be actions for controllers.)+Creating the Model
Models in Rails use a singular name, and their corresponding database tables use a plural name.
rails generate model Article title:string text:text
Rails responded by creating a bunch of files.
+Running a migration
rake db:migrate
If you would like to execute migrations in another environment, for instance in production, you must explicitly pass it when invoking the command: bin/rake db:migrate RAILS_ENV=production.
+Saving data in the controller
def create
@article = Article.new(parames[:article])
@article.save
redirect_to @article
end
The destroy method is generally the last CRUD action in the controller, and like the other public CRUD actions, it must be placed before any private or protected methods.
index, show, new, edit, create, update, destroy
In general, Rails encourages using resources objects instead of declaring routes manually.
+Creating a related model
rails generate model Comment commenter:string body:text article:references
+Associating Models
class Article < ActiveRecord::Base
has_many :comments
validates :title, presence: true, length: {minimum: 5}
end
+Creating a nested resource withing articles
resources :articles do
resources :comments
end
+Deleting Associated Objects
class Article < ActiveRecord::Base
has_many :comments, dependent: :destroy
validates :title, presence: true, length: { minimum: 5 }
end
+Rails Guides
rake doc:guides
+Rails API documentation
rake doc:rails
To be able to generate the Rails Guides locally with the doc:guides rake task you need to install the RedCloth and Nokogiri gems. Add it to your Gemfile and run bundle install and you're ready to go.