Devise
Devise 是一个 Rails 热门的 gem, 专门用来做“用户登录系统”
Gemfile文件添加
gem 'devise'
安装
bundle install
产生用户登录系统的必要文件
rails g devise:install
rails g devise user
rails db:migrate
rails g devise:views
一个用户登录系统产生了
主要用在某些action前进行是否已登录的验证
before_action :authenticate_user! , only: [:xxx] #xxx代表某些action
分页
will_paginate
Gemfile文件添加
gem 'will_paginate'
安装
bundle install
用法
paginate(:page => params[:page], :per_page => 5) 在某个action下使用,如:
@posts = @posts.paginate(:page => params[:page], :per_page => 5)
然后在视图中渲染
<%= will_paginate @posts %>
bootstrap样式的will_paginate
will_paginate-bootstrap
Gemfile文件添加
gem 'will_paginate-bootstrap'
安装
bundle install
paginate(:page => params[:page], :per_page => 5) 在某个action下使用,如:
@posts = @posts.paginate(:page => params[:page], :per_page => 5)
然后在视图中渲染
<%= will_paginate @posts, renderer: BootstrapPagination::Rails %>
simple_form--让表单写法更简单
Gemfile文件添加
gem 'simple_form'
安装
bundle install
安装 simple_form for bootstrap 的设定
rails generate simple_form:install --bootstrap
在视图中使用
<%= simple_form_for @group do |f| %>
<%= f.input :title, input_html: { class: "form-control"} %>
<%= f.input :description, input_html: { class: "form-control"} %>
<%= f.submit "Submit", class: "btn btn-primary", data: { disable_with: "Submiting..." } %>
<% end %>
字体图标
Gemfile文件添加
gem 'font-awesome-rails'
安装
bundle install
application.scss中添加
*= require font-awesome
@import "font-awesome";
在视图中使用
<%= link_to(content_tag(:i, '登出', class: 'fa fa-sign-out'), destroy_user_session_path, method: :delete) %>
上传文档
Gemfile文件添加
gem 'carrierwave'
安装
bundle install
添加字段
rails g migration add_attachment_to_your_model_name(复数形式)
在最新的迁移文件添加字段
class AddAttachmentToResume < ActiveRecord::Migration[5.0]
def change
add_column :your_model_name(复数形式), :attachment, :string
end
end
运行迁移
rails db:migrate
生成上传文件的uploader
rails g uploader attachment
在your_model挂载
class YourModelName < ApplicationRecord
mount_uploader :attachment, AttachmentUploader
validates :content, presence: true
end
在视图使用
<%= simple_form_for [@job, @resume] do |f| %>
<%= f.input :content %>
<%= f.input :attachment %>
<%= f.submit "送出" %>
<% end %>
在.gitignore文件忽略public/uploads文件
public/uploads
model注解--annotate(版本变化可能导致命令错误,请以github稳准)
安装
gem 'annotate'
安装
bundle install
生成注解
annotate --exclude tests,fixtures,factories,serializers
图片上传
安装系统支持包
sudo apt-get install imagemagick
Gemfile文件添加
gem 'carrierwave'
gem 'mini_magick'
安装
bundle install
生成image uploader
rails g uploader image
生成migration
rails g migration add_image_to_your_model
添加字段
add_column :your_models, :image, :string
运行迁移
rake db:migrate
挂载
class your_model < ApplicationRecord
mount_uploader :image, ImageUploader
end
图片上传设置
class ImageUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
process resize_to_fit: [800, 800]
version :thumb do
process resize_to_fill: [200,200]
end
version :medium do
process resize_to_fill: [400,400]
end
在your_controller设置白名单
def product_params
params.require(:product).permit(:title, :description, :quantity, :price, :image)
end
在视图使用
<%= f.input :price %>
<% if @product.image.present? %>
目前商品图
<%= image_tag(@product.image.thumb.url) %>
<% end %>
<%= f.input :image, as: :file %>
在.gitignore忽略上传的文件
public/uploads
显示图片
<% @products.each do |product| %>
<%= link_to product_path(product) do %>
<% if product.image.present? %>
<%= image_tag(product.image.thumb.url, class: "thumbnail") %>
<% else %>
<%= image_tag("http://placehold.it/200x200&text=No Pic", class: "thumbnail") %>
<% end %>
<% end %>
<%= product.title %> ¥ <%= product.price %>
<% end %>
Debug神器
Gemfile添加
group :development do
gem 'pry'
gem 'awesome_rails_console'
end
安装
bundle install
在需要的地方
binding.pry # 控制器使用
<% binding.pry %> # 视图使用
后台调整排序的功能
Gemfile添加
gem 'acts_as_list' # https://github.com/brendon/acts_as_list/tree/v0.9.13
安装
bundle install
生成migration
rails g migration AddPositionToYourModel position:integer
添加字段
class AddPositionToPost < ActiveRecord::Migration[5.2]
def change
add_column :posts, :position, :integer
Post.order(:updated_at).each.with_index(1) do |post, index|
post.update_column :position, index
end
end
end
运行迁移
rails db:migrate
在your_model
class Post < ApplicationRecord
acts_as_list
end
在控制器使用
@post = Post.find(params[:id)
@post.move_lower # 向上上移一位
@post.move_higher # 向下移一位
@post.move_to_top # 置顶
@post.move_to_bottom # 置底
使用position字段排序
@posts = Post.all.order("position ASC)
有限状态机(用于订单状态的更改)
Gemfile文件添加
gem 'aasm' # 参考网站 https://fullstack.qzy.camp/posts/698
安装
bundle install
生成migration
rails g migration add_aasm_state_to_order
添加字段
class AddAasmStateToOrder < ActiveRecord::Migration
def change
add_column :orders, :aasm_state, :string, default: "order_placed"
add_index :orders, :aasm_state
end
end
your_model添加AASM
class Order < ApplicationRecord
include AASM
aasm do
state :order_placed, initial: true
state :paid
state :shipping
state :shipped
state :order_cancelled
state :good_returned
event :make_payment, after_commit: :pay! do
transitions from: :order_placed, to: :paid
end
event :ship do
transitions from: :paid, to: :shipping
end
event :deliver do
transitions from: :shipping, to: :shipped
end
event :return_good do
transitions from: :shipped, to: :good_returned
end
event :cancel_order do
transitions from: [:order_placed, :paid], to: :order_cancelled
end
end
end
在控制器使用
@order = Order.find(params[:id)
@order.make_payment!
@orde.ship!
@order.deliver!