首先安装ruby和rails,版本如下所示
root$ ruby -v ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-linux] root$ rails -v Rails 3.2.13
gem工具安装rails时(gem install rails)出现openssl和zlib错误,如下操作
$ sudo apt-get install openssl libssl-dev $ cd path/to/your ruby source dir/ext/openssl $ sudo ruby extconf.rb $ sudo make &&sudo make install $ sudo apt-get install zlib-bin zlib1g-dev $ cd path/to/your ruby source dir/ext/zlib $ ruby extconf.rb –with-zlib-include=/usr/include –with-zlib-lib=/usr/lib $ make && sudo make install
安装配置mysql的ruby库
sudo apt-get install libmysql-ruby libmysqlclient-dev
sudo gem install mysql2 -v '0.3.11'
以mysql为默认数据库创建项目
rails new demo -d mysql
cd demo
vi config/database.yml #配置mysql账号密码(root登录修改,重启mysqld服务)
root$ rake db:create --trace#创建数据库
demo_development already exists
demo_test already exists
rails server -d 在后台启动服务(-e可以指定数据库环境)
rails3创建model及修改数据方式如下:
root$ rails generate model User root$ rails generate model Meeting root$ rails generate model Presentation class CreateUsers < ActiveRecord::Migration def change create_table :users do |t| t.string :email t.string :password, :limit => 40 t.string :name t.text :profile, :text t.boolean :admin, :default => false t.timestamps end end end class CreateMeetings < ActiveRecord::Migration def change create_table :meetings do |t| t.date :meets_on t.string :location t.text :description t.timestamps end end end class CreatePresentations < ActiveRecord::Migration def change create_table :presentations do |t| t.integer :meeting_id t.integer :user_id t.string :title t.text :description t.timestamps end # add index to columns used in joins add_index :presentations, :meeting_id add_index :presentations, :user_id end end root$ rake db:migrate sudo gem install -v=0.4.2 rb-readline class Meeting < ActiveRecord::Base # attr_accessible :title, :body attr_accessible :meets_on, :location, :description end
需要配置attr_accessible属性,不然下面的操作会提示属性不可访问的错误
rails console >>meeting = Meeting.new(:meets_on => '2007-12-06', :location => 'The Library', :description => 'Rails Hackfest') >>meeting.save >>meeting.id >>meeting = Meeting.new(:meets_on => '2008-11-03', :location => 'The School', :description => 'Dance') >>meeting.save >>meeting.id
View部分的代码如下:
<h1>Meetings</h1> <% if flash[:notice] %> <div id="flash_notice" ><%=h flash[:notice] %></div> <% end %> <div class="meeting_list" > <h2>Upcoming Meetings</h2> <ul> <% for meeting in @upcoming_meetings %> <li> <%= link_to h(meeting.name), :controller => "meetings" , :action => "show" , :id => meeting.id %> </li> <% end %> <%= no_meetings(@upcoming_meetings) %> </ul> </div> <p class="add" > <%= link_to "add meeting" , :controller => "meetings" , :action => "new" %> </p> <div class="meeting_list" > <h2>Past Meetings</h2> <ul> <% for meeting in @past_meetings %> <li> <%= link_to h(meeting.name), :controller => "meetings" , :action => "show" , :id => meeting.id %> </li> <% end %> </ul> </div>
Controller的代码如下:
class MeetingsController < ApplicationController def index @upcoming_meetings = Meeting.upcoming_meetings @past_meetings = Meeting.past_meetings end def otheraction # ... end end
Model代码如下:
class Meeting < ActiveRecord::Base # attr_accessible :title, :body attr_accessible :meets_on, :location, :description def self.upcoming_meetings find(:all, :conditions => "meets_on > CURRENT_TIMESTAMP()" , :order => "meets_on" ) end def self.past_meetings find(:all, :conditions => "meets_on <= CURRENT_TIMESTAMP()" , :order => "meets_on" ) end def name meets_on.to_s(:long) end end
访问http://127.0.0.1:3000/meetings/index即可查看mysql中的数据