最近想学ruby on rails在网上也没找到什么入门的笔记教程,其它全是电子书的下载,现有的笔记也只是最基本的。
要不就是些配置教程。
在《practical rails social networking sites ruby on rals》书上看了些,觉得不错,写出来分享一下。
首先建立一个平台$ rails railscoders --database=mysql #指定数据库为mysql
在数据库的导入之前先建立一个数据库
$ mysql -u root -p
password
>create database railscoders_development
此时测试一下数据库是否可以迁移
$ rake db:migrate
如果没有错误信息出现,恭喜你,迁移成功了!
解释一下:
(1)这里的数据库配置文件在
../railscoders/config/database.yml
在这个文件中你可能需要修改你数据库的密码!!!
(2)如果你想了解一下rake
你可以:
$ rake -T
你就可以看到你的rake命令可以做哪些事情了,哈哈
##########################
贴一下我的输出
(in /home/hugewave/myprograme/rails/first)
rake db:abort_if_pending_migrations # Raises an error if there are pending migrations
rake db:charset # Retrieves the charset for the current environment's database
rake db:collation # Retrieves the collation for the current environment's database
rake db:create # Create the database defined in config/database.yml for the current RAILS_ENV
rake db:create:all # Create all the local databases defined in config/database.yml
rake db:drop # Drops the database for the current RAILS_ENV
rake db:drop:all # Drops all the local databases defined in config/database.yml
rake db:fixtures:identify # Search for a fixture given a LABEL or ID.
rake db:fixtures:load # Load fixtures into the current environment's database.
rake db:migrate # Migrate the database through scripts in db/migrate and update db/schema.rb by invoking db:schema:dump. Target specific version with VERSION=x. Turn off output with VERBOSE=false.
rake db:migrate:down # Runs the "down" for a given migration VERSION.
rake db:migrate:redo # Rollbacks the database one migration and re migrate up.
rake db:migrate:reset # Resets your database using your migrations for the current environment
rake db:migrate:up # Runs the "up" for a given migration VERSION.
rake db:reset # Drops and recreates the database from db/schema.rb for the current environment and loads the seeds.
rake db:rollback # Rolls the schema back to the previous version.
rake db:schema:dump # Create a db/schema.rb file that can be portably used against any DB supported by AR
rake db:schema:load # Load a schema.rb file into the database
rake db:seed # Load the seed data from db/seeds.rb
rake db:sessions:clear # Clear the sessions table
rake db:sessions:create # Creates a sessions migration for use with ActiveRecord::SessionStore
rake db:setup # Create the database, load the schema, and initialize with the seed data
rake db:structure:dump # Dump the database structure to a SQL file
rake db:test:clone # Recreate the test database from the current environment's database schema
rake db:test:clone_structure # Recreate the test databases from the development structure
rake db:test:load # Recreate the test database from the current schema.rb
rake db:test:prepare # Check for pending migrations and load the test schema
rake db:test:purge # Empty the test database
rake db:version # Retrieves the current schema version number
rake doc:app # Build the RDOC HTML Files
rake doc:clobber_app # Remove rdoc products
rake doc:clobber_plugins # Remove plugin documentation
rake doc:clobber_rails # Remove rdoc products
rake doc:guides # Generate Rails guides
rake doc:plugins # Generate documentation for all installed plugins
rake doc:rails # Build the RDOC HTML Files
rake doc:reapp # Force a rebuild of the RDOC files
rake doc:rerails # Force a rebuild of the RDOC files
rake gems # List the gems that this rails application depends on
rake gems:build # Build any native extensions for unpacked gems
rake gems:build:force # Force the build of all gems
rake gems:install # Installs all required gems.
rake gems:refresh_specs # Regenerate gem specifications in correct format.
rake gems:unpack # Unpacks all required gems into vendor/gems.
rake gems:unpack:dependencies # Unpacks all required gems and their dependencies into vendor/gems.
rake log:clear # Truncates all *.log files in log/ to zero bytes
rake middleware # Prints out your Rack middleware stack
rake notes # Enumerate all annotations
rake notes:custom # Enumerate a custom annotation, specify with ANNOTATION=WTFHAX
rake notes:fixme # Enumerate all FIXME annotations
rake notes:optimize # Enumerate all OPTIMIZE annotations
rake notes:todo # Enumerate all TODO annotations
rake rails:freeze:edge # Lock to latest Edge Rails, for a specific release use RELEASE=1.2.0
rake rails:freeze:gems # Lock this application to the current gems (by unpacking them into vendor/rails)
rake rails:template # Applies the template supplied by LOCATION=/path/to/template
rake rails:unfreeze # Unlock this application from freeze of gems or edge and return to a fluid use of system gems
rake rails:update # Update both configs, scripts and public/javascripts from Rails
rake rails:update:application_controller # Rename application.rb to application_controller.rb
rake rails:update:configs # Update config/boot.rb from your current rails install
rake rails:update:generate_dispatchers # Generate dispatcher files in RAILS_ROOT/public
rake rails:update:javascripts # Update your javascripts from your current rails install
rake rails:update:scripts # Add new scripts to the application script/ directory
rake routes # Print out all defined routes in match order, with names.
rake secret # Generate a crytographically secure secret key.
rake stats # Report code statistics (KLOCs, etc) from the application
rake test # Run all unit, functional and integration tests
rake test:benchmark # Run tests for benchmarkdb:test:prepare / Benchmark the performance tests
rake test:functionals # Run tests for functionalsdb:test:prepare / Run the functional tests in test/functional
rake test:integration # Run tests for integrationdb:test:prepare / Run the integration tests in test/integration
rake test:plugins # Run tests for pluginsenvironment / Run the plugin tests in vendor/plugins/*/**/test (or specify with PLUGIN=name)
rake test:profile # Run tests for profiledb:test:prepare / Profile the performance tests
rake test:recent # Run tests for recentdb:test:prepare / Test recent changes
rake test:uncommitted # Run tests for uncommitteddb:test:prepare / Test changes since last checkin (only Subversion and Git)
rake test:units # Run tests for unitsdb:test:prepare / Run the unit tests in test/unit
rake time:zones:all # Displays names of all time zones recognized by the Rails TimeZone class, grouped by offset.
rake time:zones:local # Displays names of time zones recognized by the Rails TimeZone class with the same offset as the system local time
rake time:zones:us # Displays names of US time zones recognized by the Rails TimeZone class, grouped by offset.
rake tmp:cache:clear # Clears all files and directories in tmp/cache
rake tmp:clear # Clear session, cache, and socket files from tmp/
rake tmp:create # Creates tmp directories for sessions, cache, sockets, and pids
rake tmp:pids:clear # Clears all files in tmp/pids
rake tmp:sessions:clear # Clears all files in tmp/sessions
rake tmp:sockets:clear # Clears all files in tmp/sockets
##########################
这里我们先跳过页面布局和CSS配置,这又是一大段的内容,而且这个我还很不了解,嘿嘿
建立一个Page模型来让我导入我们的数据库内容
$ ruby script/generate model Page
生成了模型,接着让我们来改一下我们的配置文件吧
../railscoder/db/migrate/20100326071901_create_pages.rb
注释一下,你的文件名可能不是这个,不过路径是这个,哈哈,好像目前也就只有这个文件
进去看一下,你会看见两个定义
def self.up
def self.down
把这个文件的内容改成这样:
class CreatePages < ActiveRecord::Migration
def self.up
create_table :pages do |t|
t.column :title, :string
t.column :permalink, :string
t.column :body, :text
t.column :created_at, :datetime
t.column :updated_at, :datetime
end
end
def self.down
drop_table :pages
end
end
可能你看这个还不太明白,那再改一下,把self.up方法改成这样:
def self.up
create_table :pages do |t|
t.column :title, :string
t.column :permalink, :string
t.column :body, :text
t.column :created_at, :datetime
t.column :updated_at, :datetime
end
Page.create(:title => "RailsCoders Home",
:permalink => "welcome-page",
:body => "Welcome to RailsCoders")
end
这样一来你大概已经明白差不多了吧。
就是建立一张表,包括什么内容呢?
就是包括了,title,permalink,body,create_at,update_at
其实如果你进数据库,类似的操作就是:
>create table pages(
>title varchar(200),
>permalink varchar(200),
>body varchar(200),
>create_at time,
>update_at time);
这样你就该明白了吧,哈哈,原来这么简单,用ruby导入数据就变得超级简单了
不过不要高兴的太早,你还没导进去呢!怎么进去,写个命令:
$ rake db:migrate
输出信息,会告诉你成功了的,哈哈
如果你不信你可以进去看一下
mysql> use railscoders_development;
mysql> select * from pages;
你是不是已经看到那个文件里面导入的信息了,哈哈!
好了,数据的迁移这样就结束了
##########################
最后贴一下我的ruby on rals 配置
Ruby version | 1.8.7 (i486-linux) |
RubyGems version | 1.3.5 |
Rack version | 1.0 |
Rails version | 2.3.5 |
Active Record version | 2.3.5 |
Active Resource version | 2.3.5 |
Action Mailer version | 2.3.5 |
Active Support version | 2.3.5 |
Application root | /home/hugewave/myprograme/rails/first |
Environment | development |
Database adapter | sqlite3 |
Database schema version | 0 |
##########################