易斯灵软件(eSysWorks)是Ruby on Rails敏捷开发的推崇者,在此,我们整理了大量有用的RoR插件供国内开发者参考:
1. Authlogic 身份认证验证框架
sudo gem install authlogic
rails plugin install git://github.com/binarylogic/authlogic.git
@articles = Article.all(:order => 'published_at DESC').paginate(:page => params[:page], :per_page => 8)
<%= will_paginate @articles, :previous_label=>"« 上一页",:next_label=>'下一页 »' %>
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| -%>