常用GEM的使用方法

常用GEM的使用方法

1. kaminari 分页系统

1.1

gem 'kaminari'

bundle

rails g kaminari:config

rails g kaminari:views default/bootstrap3

1.2

lists_controller.rb

defindex@lists=List.all.order(created_at::desc).page(params[:page]).per(5)end

1.3

<%= paginate @lists %>

1.4

en:  hello: "Hello world"  views:    pagination:      first: "首页"      last: "末页"      previous: "上一页"      next: "下一页"      truncate: "..."

2. devise 用户登陆注册系统

2.1

gem devise

bundle

rails g devise:install

rails g devise User

rails g devise:views

3. cancancan

3.1

gem 'cancancan'

bundle

rails g cancan:ability

ability.rbifuser.admin?can:manage,:allelseend

4. redcarpet 文本支持 markdown

gem 'redcarpet'

bundle

接下来,打开app/helper/application_helper.rb, 添加下列代码。defmarkdown(text)options={:autolink=>true,:space_after_headers=>true,:fenced_code_blocks=>true,:no_intra_emphasis=>true,:hard_wrap=>true,:strikethrough=>true}markdown=Redcarpet::Markdown.new(HTMLwithCodeRay,options)markdown.render(h(text)).html_safeendclassHTMLwithCodeRay2)endend

5. carrierwave 图片上传

gem 'carrierwave', '0.6.2'

gem 'mini_magick' #不使用rmagick,占内存

bundle

rails g migration add_avatar_to_users avatar:string

rake db:migrate

rails generate uploader Avatar [将会生成文件app/uploaders/avatar_uploader.rb]

mount_uploader :avatar, AvatarUploader [为user的model user.rb添加如下代码,使表之间关联]

修改app/uploaders/avatar_uploader.rb

如何在表单中上传<%=form_for(@user)do|f|%>    <%= f.file_field :avatar %>

    <%= f.hidden_field :avatar_cache %>

 

...

如何显示图片<%=image_tag(@user.avatar_url(:large))[email protected]%>[这里的:large就是指定图片的版本为large64x64大小]

6. bootstrap-sass

gem 'bootstrap-sass'

bundle

往 assets 里的 css 和 js 添加 引用

重新 rails s

7. simple_form

gem 'simple_form'

bundle

rails g simple_form:install

你可能感兴趣的:(常用GEM的使用方法)