易斯灵软件(eSysWorks)是Ruby on Rails敏捷开发的推崇者,在此,我们整理了大量有用的RoR插件供国内开发者参考:
1. Authlogic 身份认证验证框架
Authlogic网址:?https://github.com/binarylogic/authlogic/?
Authlogic的安装:
Rails3:
sudo gem install authlogic
插件方式安装:
rails plugin install git://github.com/binarylogic/authlogic.git
Authlogic实例:?https://github.com/binarylogic/authlogic_example?
2.?Will_Paginate?分页插件
Will_Paginate是一个巨好用的查询分页插件,强烈推荐使用!
will_paginate网址:?https://github.com/mislav/will_paginate?
使用示例:
在Controller的Action中:
@articles = Article.all(:order => 'published_at DESC').paginate(:page => params[:page], :per_page => 8)
在VIEW中:
<%= will_paginate @articles, :previous_label=>"« 上一页",:next_label=>'下一页 »' %>
?
3.?Paperclip?上传插件
Paperclip是一款Rails插件,用来实现文件上传功能,如头像上传等。
Paperclip网址:?https://github.com/thoughtbot/paperclip?
教学视频:http://railscasts.com/episodes/134-paperclip
使用示例:
安装成功之后,做DB Migration, 如下所示加入上传图片字段:
class AddPhotoToEvent < ActiveRecord::Migration def self.up add_column :events, :photo_file_name, :string add_column :events, :photo_content_type, :string add_column :events, :photo_file_size, :integer end def self.down remove_column :events, :photo_file_name remove_column :events, :photo_content_type remove_column :events, :photo_file_size end end
?在Model中:
class Event < ActiveRecord::Base belongs_to :user validates_presence_of :title, :on => :create, :message => "can't be blank" validates_presence_of :teaser, :on => :create, :message => "can't be blank" validates_presence_of :subject, :on => :create, :message => "can't be blank" # Paperclip has_attached_file :photo, ??:styles => { ????:thumb=> "100x100#", ????:small? => "150x150>", ????:medium => "300x300>", ????:large =>?? "400x400>" }
?在View中:
<% form_for(@event,:html => { :multipart => true }) do |f| %> ??<%= f.error_messages %> ??<%= render :partial => 'form', :locals => { :f => f } %> <% end %>
?_form文件中加入上传代码:
<p> ??<%= f.label 'Photo' %> ??<%= f.file_field :photo %> </p>
?此时,基本上传功能配置完成。
?
4. Client_side_validations?客户端验证插件
客户端校验是Web开发必需的,client_side_validations就是这样一款验证表单的插件。
client_side_validations网址:https://github.com/bcardarella/client_side_validations?
注意此插件依赖于JQuery,版本1.4.1及以上适用。
在application.html.erb中加入:
<%= javascript_include_tag 'jquery', 'rails.validations'-%>
?在表单中加入:
<%= form_for @book, :validate => true do |book| -%>