可以免去重复的view代码
说明: 将常用的View公用, 统一了I18n
# Controller class BlogController < InheritedResources::Base dry_view :columns => [:title, :body] end
# Model class Blog def label self.title end end
script/plugin install git://github.com/josevalim/inherited_resources.git script/plugin install git://github.com/alvin2ye/activerecord_dom_helper.git
其中 form 的布局和 Css 来自 formtastic (github.com/justinfrench/formtastic/tree/master)
1 ./script/plugin install git://github.com/alvin2ye/dry_view.git 2 ./script/plugin install git://github.com/josevalim/inherited_resources.git 3 ./script/generate Model Company name:string addr:string 4 rake db:migrate 5 ruby script/generate controller companies 6
# app/controllers/companies_controller.rb class CompaniesController < InheritedResources::Base dry_view end
# app/models/company.rb class Company < ActiveRecord::Base def label self.name end end
# config/routes.rb map.resources :companies
7 rake dry_view:install 8 add to layout: <%= stylesheet_link_tag “formtastic” %>
# config/locales/zh.yml zh: dry_view: actions: “操作” view: “查看” edit: “编辑” remove: “删除” new: “新建” submit: “提交” required: “必填” cancel: “取消” return_index: “返回列表” are_you_sure: “你确认吗?”
class CompaniesController < InheritedResources::Base
# Only display this columns, default is all columns except [:id, :created_at, :updated_at] dry_view :columns => [:name, :code], :except_show => true def set_dry_view_config # You can set @dry_view here end
end
class Company < ActiveRecord::Base
def allow_show? ... end def allow_edit? ... end def allow_destroy? ... end
end
form
<%= render :partial => “#{c.name}_form_column”, :locals => {:column => c} %> so the file must named like: app/views/books/_name_form_column.html.erb
# app/helpers/books_helper.rb
def name_column(record) “ override #{record.name} ” end
Issue at agideo.lighthouseapp.com/projects/34756-dry_view/overview Copyright © 2009 Alvin Ye at Agideo, released under the MIT license
alvin.ye.cn(*)gmail.com