常用gem的安装使用

常用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

IdNameDescriptionReferences
1foreman启动管理
2annotate数据库字段注释
3seedbank自定义 seed 数据
4rails-i18n多语言处理
5paranoia软删除
6semantic-ui-sassUI 框架Official
7guard-livereload / rack-livereload浏览器自动刷新
8pry-rails / pry-remote调试环境pry
9slim-rails模板引擎Official html2slim
10rspec-rails / fuubar行为驱动开发Official
11factory_girl_rails基础数据
12guard-rspec / spring-commands-rspec自动运行测试
13faker随机生成测试数据
14shoulda-matchers常用测试匹配Official
15capistrano-rails / capistrano3-puma自动部署Official
16timecop设定时间范围
17whenever定时任务Whenever 使用筆記
18database_cleaner清理测试数据库
19capybara功能测试
20launchy打开浏览器
21dotenv-rails环境变量管理
22enumerize枚举类型管理(带语言包)
23rack-cors支持跨域请求Rails中跨域请求的解析
24active_model_serializersJSON 数据生成用 JSON 构建 API 的标准指南
25chinese_number解析汉语描述的数字,转换成阿拉伯数字
26bootstrap-sassUI 框架Official
27chinese_pinyin中文转换为拼音
28rest-client模拟登录网页,返回结果
29sidekiqs/capistrano-sidekiq队列Official
30aasm状态管理example
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值