[ruby on rails]一些常用的gems

一些好的gem推荐:https://ruby-china.org/wiki/gems

1. 图形验证码

rucaptcha
使用方法:http://docs.qzy.camp/docs/%E4%BD%BF%E7%94%A8gem-rucaptcha

2. 计算网页浏览量

impressionist: https://github.com/charlotte-ruby/impressionist

3. 分页

pagy: https://ddnexus.github.io/pagy/how-to
kaminari: https://github.com/kaminari/kaminari

kaminari设置中文显示:在en.yml中en后面添加

 views:
    pagination:
      first: "« 首页"
      last: "末页 »"
      previous: "« 上一页"
      next: "下一页 »"
      truncate: "..."

4. 富文本编辑器

ckeditor: https://github.com/galetahub/ckeditor

5. console美观适合人看

awesome_rails_console: https://github.com/ascendbruce/awesome_rails_console

6. 高效读取excel

creek:https://github.com/pythonicrubyist/creek
roo: https://github.com/roo-rb/roo

7. 导出excel、csv

spreadsheet_architect: https://github.com/westonganger/spreadsheet_architect

用法:
(1)在ApplicationRecord中加入 include SpreadsheetArchitect

class ApplicationRecord < ActiveRecord::Base
  include SpreadsheetArchitect
end

(2)在model中定义spreadsheet_columns方法,同时设置导出到excel中的格式

class Movie < ApplicationRecord
  
  def spreadsheet_columns
    [
      ['Id', :id],
      ['标题', :title],
      ['时间', :created_at]
    ]
  end
  
  SPREADSHEET_OPTIONS = {
    spreadsheet_columns: :spreadsheet_columns,
    header_style: {background_color: 'AAAAAA', color: 'FFFFFF', align: :center, font_name: 'Arial', font_size: 10, bold: true, italic: false, underline: false},
    row_style: {background_color: nil, color: '000000', align: :left, font_name: 'Arial', font_size: 10, bold: false, italic: false, underline: false},
    sheet_name: self.name,
    column_styles: [],
    range_styles: [],
    conditional_row_styles: [],
    merges: [],
    borders: [],
    column_types: [],
  }
end

(3)controller中使用

def index
    @movies = Movie.where.not("title = ?", '')
    respond_to do |format|
      format.html
      format.xlsx {render xlsx: @movies , filename:"movies-#{Time.now.to_s}"}
    end
end

(4)下载excel

 #在view中
 <%= link_to 'download',index_movies_path  %>

8. 表单

simple_form: https://github.com/plataformatec/simple_form

9. 图标

bootstrap: https://github.com/twbs/bootstrap-sass
fontawesome:http://www.fontawesome.com.cn/get-started/
gem ‘jquery-rails’
gem ‘bootstrap-sass’
gem ‘font-awesome-sass’

#application.scss

@import "bootstrap-sprockets";
@import 'bootstrap';
@import "font-awesome-sprockets";
@import "font-awesome";

#devise中让notice的样式继承alert-success,让alert样式继承alert-danger
.alert-notice{
  @extend .alert-success;
}
.alert-alert{
   @extend .alert-danger;
}
#application.js
//= require jquery
//= require bootstrap-sprockets
#提交表单时候有个转圈图标
<%= f.button "确认", data:{ disable_with: "<span class='fa fa-spinner fa-spin'></span>请稍等..." } %>

10. 自定义排列顺序acts_as_list

https://github.com/swanandp/acts_as_list

用法:在需要自定义排序的model添加position栏位

class AddPositionToEvents < ActiveRecord::Migration[5.0]
  def change
    add_column :events, :position, :integer
  end
    add_index :events, :position
    
    Event.order(:updated_at).each.with_index(1) do |event, index|
      event.update_column :position, index
    end
end

在model中加入acts_as_list

class Event < ApplicationRecord
	acts_as_list
end

routes中添加路由

resources :events do
	collection do
	    post :bulk_update
    end
    member do
        patch :move_up
        patch :move_down
    end
end

controller中添加move_up , move_down

def move_up
    @event = Event.find(params[:id])
    @event.move_higher
    flash[:success] = 'move up success'
    redirect_to  admin_events_path

  end

  def move_down
    @event = Event.find(params[:id])
    @event.move_lower
    flash[:success] = 'move down success'
    redirect_to admin_events_path

  end

view中添加按钮

 <%= link_to 'move Up', move_up_admin_event_path(event), method: :patch,class:'btn btn-default'  %>
 <%= link_to 'move Down', move_down_admin_event_path(event), method: :patch,class:'btn btn-default'  %>

11. 拖拉UI

gem ‘jquery-ui-rails’

12. 赞、喜欢、收藏、关注、订阅、屏蔽等 action-store

https://github.com/rails-engine/action-store

13. 用户通知 notifications

https://github.com/rails-engine/notifications

14. http抓包 rest-client 配合nokogiri使用

#rest-clent
https://github.com/rest-client/rest-client
#nokogiri
https://github.com/sparklemotion/nokogiri

15. 检查N+1查询 bullet

https://github.com/flyerhzm/bullet

用法:

gem 'bullet', group: 'development'
 rails g bullet:install
#config/environments/development.rb
 config.after_initialize do
    Bullet.enable = true
    Bullet.alert = true
 end

16. 代码质量分析 rubycritic

https://github.com/whitesmith/rubycritic

17. 代码质量风格分析 按ruby风格指南分析 rubocop

https://github.com/rubocop-hq/rubocop

18. pdf生成器 prawn

https://github.com/prawnpdf/prawn

19. 生成现有数据关系 rails-erd

https://github.com/voormedia/rails-erd

20. rails_best_practices

https://github.com/flyerhzm/rails_best_practices

  • 查看哪些文件都有什么问题
bundle exec rails_best_practices .
  • 可以自定义配置
# 在config生成配置文件rails_best_practices.yml
rails_best_practices -g
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值