常用gem的安装使用
annotate[注解]
gem 'annotate', '~> 2.7', '>= 2.7.1'
$ rails g annotate:install
seedbank[seed分表]
gem 'seedbank', '~> 0.4.0'
创建文件:db/seeds/development/modles.seeds.rb
puts "添加字段"
Member.create(:name => "马飞虎", :telphone => "14797478668", :age => "25")
或
Member.create(name: "马飞虎", telphone: "14797478668", age: "25")
执行命令:
rails db:send
slim-rails[视图简化]
gem 'slim-rails', '~> 3.1', '>= 3.1.1'
rails-i18n[中英文转化]
gem 'rails-i18n', '~> 5.0', '>= 5.0.1'
config/application.rb
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
config.i18n.available_locales = ['zh-CN', :en]
config.i18n.default_locale = 'zh-CN'
创建文件config/locals/zh-CN.yml
zh-CN:
activerecord:
models:
member: "成员"
attributes:
member:
name: 名字
telphone: 手机号码
age: 年龄
livereload[自动刷新页面]
gem 'rack-livereload', '~> 0.3.16' and 'gem 'guard-livereload', '~> 2.5', '>= 2.5.2'
初始化: guard init livereload
config/environments/development.rb
添加:
config.middleware.insert_after ActionDispatch::Static, Rack::LiveReload
bundle exec guard
foreman[多服务器启动]
gem 'foreman', '~> 0.82.0'
在根目录创建Procfile文件
web: bundle exec rails s
$ foreman start
pry-rails[错误调试]
gem 'pry-rails', '~> 0.3.4'
bingding.pry
pry-remote[多服务开启调试与foreman共用]
gem 'pry-remote', '~> 0.1.8'
binding.remote_pry
根据提示:新开页面操作
semantic-ui-sass[css 及js 框架]
application.css.scss
修改文件:application.css为application.css.scss
添加:
@import "semantic-ui";
application.js
//= require semantic-ui
paperclip
查看image扩展
which convert /usr/local/bin/convert
配置迁移文件
class CreateProductImages < ActiveRecord::Migration[5.0] def change create_table :product_images, comment: "商品图片表" do |t| t.belongs_to :product,comment: "创建关联关系id字段" t.integer :weight, default:0, comment: "可控图片信息" t.attachment :image, commet: "image的原始数据" t.timestamps end end end
配置图片的属性
class ProductImage < ApplicationRecord belongs_to :product #对图片进行设置 has_attached_file :image, styles:{ small: '60^x60', middle: '200^x200', big: "960x" } validates_attachment_content_type :image, content_type:/\Aimage\/.*\Z/ validates_attachment_size :image, in: 0..5.megabytes end
sorcery
rails g sorcery:install
rails g sorcery:install user_activation remember_me reset_password --only-submodules
ancestry(无限极分类)
创建模型
rails g migration add_ancestry_to_[table] ancestry:string
模型设置
has_ancestry
use
c = Category.create title: "ci"
c2 = c.children.create title: "c12"
c.children.first
c.children.first.parent
rspec-rails[基本测试]
gem 'rspec-rails', '~> 3.5'
rails generate rspec:install
重要操作
文件: spec/rails_helper.rb
去掉注释(23行): ...spec/support/**/*.rb..
第一个测试
生成模型
rails g model book name author price
spec/models/book_spec.rb
require 'rails_helper'
RSpec.describe Book, type: :model do
it "数据正确可以通过测试" do
book = Book.new(
name: 'xx',
author: 'yy',
price: 123
)
expect(book).to be_valid
end
end
运行测试
bundle exec rspec
Rspec[基本测试]
安装
group :development, :test do
gem 'rspec-rails', '~> 3.5'
end
guard-rspec(测试自动)
安装
group :development, :test do
gem 'guard-rspec', '~> 4.7'
end
bundle exec guard init rspec
bundle exec guard
shoulda-matchers[简化测试]
安装
group :development, :test do
gem 'shoulda-matchers', '~> 3.1'
end
新增文件 spec/support/shoulda_matchers.rb
# https://github.com/thoughtbot/shoulda-matchers#getting-started
RSpec.configure do |config|
Shoulda::Matchers.configure do |config|
config.integrate do |with|
# test framework
with.test_framework :rspec
# libraries
with.library :rails
end
end
end
结果显示格式(.rspec)
fuubar[简化测试页面]
--format documentation
gem 'fuubar', '~> 2.2'
.rspec
--format Fuubar
Gems table
Id | Name | Description | References |
---|---|---|---|
1 | foreman | 启动管理 | |
2 | annotate | 数据库字段注释 | |
3 | seedbank | 自定义 seed 数据 | |
4 | rails-i18n | 多语言处理 | |
5 | paranoia | 软删除 | |
6 | semantic-ui-sass | UI 框架 | Official |
7 | guard-livereload / rack-livereload | 浏览器自动刷新 | |
8 | pry-rails / pry-remote | 调试环境 | pry |
9 | slim-rails | 模板引擎 | Official html2slim |
10 | rspec-rails / fuubar | 行为驱动开发 | Official |
11 | factory_girl_rails | 基础数据 | |
12 | guard-rspec / spring-commands-rspec | 自动运行测试 | |
13 | faker | 随机生成测试数据 | |
14 | shoulda-matchers | 常用测试匹配 | Official |
15 | capistrano-rails / capistrano3-puma | 自动部署 | Official |
16 | timecop | 设定时间范围 | |
17 | whenever | 定时任务 | Whenever 使用筆記 |
18 | database_cleaner | 清理测试数据库 | |
19 | capybara | 功能测试 | |
20 | launchy | 打开浏览器 | |
21 | dotenv-rails | 环境变量管理 | |
22 | enumerize | 枚举类型管理(带语言包) | |
23 | rack-cors | 支持跨域请求 | Rails中跨域请求的解析 |
24 | active_model_serializers | JSON 数据生成 | 用 JSON 构建 API 的标准指南 |
25 | chinese_number | 解析汉语描述的数字,转换成阿拉伯数字 | |
26 | bootstrap-sass | UI 框架 | Official |
27 | chinese_pinyin | 中文转换为拼音 | |
28 | rest-client | 模拟登录网页,返回结果 | |
29 | sidekiqs/capistrano-sidekiq | 队列 | Official |
30 | aasm | 状态管理 | example |