Rails Study(I)
Getting Started with Rails
1. MVC Architecture
1.1. Models
One table in my
database will correspond to one model in my application.
The bulk of my application's bussiness logic will be concentrated in the models.
1.2. Views
Views are often HTML
files with embedded Ruby code that perform tasks related solely to the presentation of the data.
1.3. Controllers
Controllers provide the "glue" between models and views.
2. The Components of Rails
Action Pack
Action Controller
Action Dispatch
Action View
Action Mailer
Action Model
Action Record
Action Resource
Active Support
Railties
2.1 Action Pack
2.1.1 Action Controller
Action Controller processes incoming requests to a Rails application, extracts parameters, and dispatches them to the intended
action.
Services provided by Action Controller include session management, template rendering, and redirect management.
2.1.2 Action view
Action view
can create both HTML and
XML output. Action View manages rendering templates, including nested and partial
templates, and includes built-in AJAX support.
2.1.3 Action Dispatch
2.1.4 Action Mailer
A framework for building e-mail services.
2.1.5 Action Model
Action Model -----> Business Logic -----> ORM
2.1.6 Action Record
CRUD
functionality
2.1.7 Action Resource
2.1.8 Action Support
2.2 REST
3. Creating a New Rails Project
Creating a Blog Application
>rails new railsexample
or I can create a rails
project in eclipse.
type >rails -h in eclipse
command console for more information.
File Purpose
Gemfile This file allows you to specify what gem dependencies are needed for your Rails application
Readme
Rakefile This file contains batch jobs that can be run from the terminal
config.ru Rack configuration for Rack based servers used to start the application.
Folder Purpose
app Contains the controllers, models, and views for your application. You'll focus on this folder for the remainder of this
guide.
config Configure your application's runtime rules, routes, database, and more.
db Shows your current database schema
doc
lib
Extended modules for my application
log
public This is where you put your images, javasript, stylesheets(CSS), and other static files.
script Contains the rails script that starts your app and can contain other scripts you use to deploy or run applications
test
tmp
vendor A place for all third-party code. In a typical Rails application, this includes Ruby Gems, the Rails source code (if you install
it into your project) and plugins containing additional prepackaged functionality.
3.1 Installing the Required Gems
>bundle install
3.2 Configuring a Database
config/database.yml
3.2.1 Configuring an SQLite3 Database
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
3.2.2 Configuring a MySQL Database
development:
adapter: mysql2
encoding: utf8
database: example_development
pool: 5
username: root
password:
socket: /tmp/mysql.sock
3.3 Creating the Database
>rake db:create
error message:
rake aborted!
uninitialized constant Rake::DSL
d:/tool/Ruby192/lib/ruby/1.9.1/rake.rb:2482:in `const_
missing'
d:/tool/Ruby192/lib/ruby/gems/1.9.1/gems/rake-0.9.2/lib/rake/tasklib.rb:8:in `<class:TaskLib>'
Solutions:
Change the Rakefile
require File.expand_path('../config/application', __FILE__)
require 'rake/dsl_definition'
module ::Railsexample
class Application
include Rake::DSL
end
end
module ::RakeFileUtils
extend Rake::FileUtilsExt
end
Railsexample::Application.load_tasks
>bundle update
>rake db:create
error message:
uninitialized constant Mysql2
solution:
>gem install mysql2
Fetching: mysql2-0.3.6.gem (100%)
error message:
ERROR: Error installing mysql2:
The 'mysql2' native gem requires installed build tools.
Please update your PATH to include build tools or download the DevKit
from 'http://ruby
installer.org/downloads' and follow the instructions
at 'http://github.com/oneclick/rubyinstaller/wiki/Development-Kit'
solution:
Download the file DevKit-tdm-32-4.5.2-20110712-1620-sfx.exe in 7zip format from that URL
Create a directory devkit and unzip the file there with 7zip software.
Copy the directory devkit to d:/tool/devkit
>cd d:/tool/devkit
>ruby dk.rb init
This step will list the installation of ruby and generate a file named config.yml
>ruby dk.rb install
After all the steps, do install mysql2 again.
>gem install mysql2
No, but the problem is still there.
>gem install mysql2 -v 0.2.6 --platform x86-mingw32
change Gemfile and add one line
gem 'mysql2', '0.2.6'
>rake db:create
error message:
The program can't start because LIBMYSQL.dll is missing from your
computer. Try reinstalling the program to fix this problem.
solution:
Copy file D:\ProgramFiles\MySQL\My
SQL Server 5.5\lib\libmysql.dll to directory D:\tool\Ruby192\bin
try again. >rake db:create
error message:
193: %1 is not a valid Win32 application. - d:/tool/Ruby192/lib/ruby/gems/1.9.1/gems/mysql2-0.2.6-x
86-mingw32/lib/mysql2/1.9/mysql2.so
try other
ways
>gem install mysql2 -v 0.2.6 -- --with-mysql-dir="D:\ProgramFiles\MySQL\MySQL Server 5.5"
try other ways
>gem install mysql2 -- --with-mysql-lib="D:\ProgramFiles\MySQL\MySQL Server 5.5\lib" --with-mysql-include="D:\ProgramFiles\MySQL\MySQL Server 5.5\include" --with-mysql-dir="D:\ProgramFiles\MySQL\MySQL Server 5.5"
try other ways
Gemfile
gem 'mysql', '2.8.1'
adapter mysql
>bundle install
>rake db:create
>gem list -d mysql
Same problem.
Finally I download the file from http://instantrails.rubyforge.org/svn/trunk/InstantRails-win/InstantRails/mysql/bin/libmySQL.dll
and put this file to ruby_home/bin.
>rake db:create
It works.
references:
http://guides.rubyonrails.org/
https://github.com/oneclick/rubyinstaller/wiki/Development-Kit
http://www.iteye.com/topic/1001758