rails&ruby**章节:1-2**
rails 敏捷开发和 rails自然之道理念。
3-days ~
rails 开发环境搭建和IDE 选择
环境搭建: rvm ruby gem rails
IDE: rubymine / sublime / vim 快捷命令: subl . subl ./Gemfile
浏览器: chrome
git , github 使用
git命令
git -am "description"
rails new --skip-bundle 修改bundle的源为国内后,执行bundle install
rails new -d mysql 指定rail使用mysql数据库 默认使用的数据库是sqllite的
rails 项目生成之后的目录
app/controller views models MVC等
app/assert 静态资源 css js img
bin 可执行文件命令 如 bundle、rake等命令
config 配置文件 数据库yml 和 路由配置
db 数据库迁移文件
public 访问数据入口和错误跳转页面 如500error.html
test 程序的测试文件
Gemfile 程序所需要的gem ~> 保持主副版本不变,只进行bug修复的更新补丁 , >=总会升级到最新版
Gemfile 确保复制本程序使用相同版本的gem
bundler 用来安装gem 的工具
bundle update / bundle install
bundle install --without production 不安装生产环境所需要的gem
rails server 进行程序的运行
MVC GUI
ruby china好的资料:
http://guides.ruby-china.org/getting_started.html
git 进行版本控制的操作
如何不去跟踪一个文件
git环境搭建 git服务器搭建
heroku 类似的paas 平台上线真实的去跑跑项目
脚手架快车
rails generate scaffold User单数 name:string email:string
rails generate scaffold Miscropost content:text user_id:integer
执行 rake db:migrate
脚手架强大的功能思考
资源 -URI- 统一资源定位
rest风格设计 表示层状态转换
将对象模型化成具有被curd操作属性的资源,这些操作将关系型数据库的curd操作和http请求方法对应起来
资源规则路由 resources :users (Symbol)
行为 index new create update destroy show edit
控制器的继承 <
建两个模型对应好表关系在model 中进行定义
分析脚手架生成的代码和mvc 之间的关系
分析代码的执行流程
def index @posts = User.all end
Active Record 库???
限制Micropost的长度: 进入model中, 代码:
validates :content, length:{maximum:140} 限制140字节长度
定义表的关系 一个用户有多条微博 一条微博只能来自一个用户
user_model.rb
has_many :miscroposts 复数
micropost_model.rb
belongs_to :user 单数
rails console 中进行验证
first = User.first
first.miscroposts
model 和controller 继承的基类分析

浙公网安备 33010602011771号