Ruby on Rails Tutorial 第二章 之 微博资源
2015-07-05 22:16
831 查看
1、微博模型如下图所示:
2、创建微博资源,命令如下:
$ rails generate scaffold Micropost content:text user_id:integer #生成微博资源
$ bundle exec rake db:migrate #执行迁移,更新数据库,使用新建的数据模型
3、微博内容的限制
(1)限制微博长度
在app/models/micropost.rb中
(2)微博不能为空
4、一个用户拥有多篇微博
app/models/user.rb中
app/models/micropost.rb中
验证user与micropost之间的关系
5、UsersController和MicropostsController中的继承关系如下图所示:
6、部署应用
2、创建微博资源,命令如下:
$ rails generate scaffold Micropost content:text user_id:integer #生成微博资源
$ bundle exec rake db:migrate #执行迁移,更新数据库,使用新建的数据模型
3、微博内容的限制
(1)限制微博长度
在app/models/micropost.rb中
class Micropost <ActiveRecord::Base validates :content, length:{ maximum:140 } end
(2)微博不能为空
class Micropost <ActiveRecord::Base validates :content, length:{ maximum:140 } ,presence: true end
4、一个用户拥有多篇微博
app/models/user.rb中
class User <ActiveRecord::Base has_many :microposts end
app/models/micropost.rb中
class Micropost <ActiveRecord::Base belongs_to :user validates :content, length:{ maximum:140 } end
验证user与micropost之间的关系
$ rails console >> first_user = User.first >> first_user.microposts >> micropost = first_user.microposts.first >> micropost.user >> exit
5、UsersController和MicropostsController中的继承关系如下图所示:
6、部署应用
$ git status $ git add -A $ git commit -m "Finish toy app" $ git push $ git push heroku $ heroku run rake db:migrate
相关文章推荐
- Ruby on Rails Tutorial 第二章 之 用户资源&MVC&REST
- ruby里面的属性访问器
- ruby里面module和class的区别
- 如何在 OS X 中安装 ruby
- Ruby用法总结
- Windows环境搭建Web自动化测试框架Watir(基于Ruby)
- Ruby(Selenium / Rspec)在Windows 8_64上安装步骤
- ruby简单的基本 6
- 如何以非 root 用户将应用绑定到 80 端口-ssh 篇 » 社区 » Ruby China
- 使用 Capistrano 和写作 Ruby 迭代边缘部署
- 在ruby 2 和rails 4 里面使用send_file 下载文件的时候。在IE下面中文名出现乱码的处理方式
- ruby的入门基础
- ubuntu安装Ruby on rails
- CocoaPods
- ubuntu 14.04中安装ruby on rails环境(填坑版)
- Ruby Profiler 详解之 ruby-prof(I)
- Ruby高手点评Scala编程语言十大绝招
- Ruby on Rails Tutorial 第一章 之 Heroku部署
- Ruby on Rails Tutorial 第一章 之 Git项目管理
- Ruby on Rails Tutorial 第一章 之 搭建开发环境