插件activescaffold 生成数据显示 用法示例

阅读更多
这个插件的主要作用是用简单的两三行代码生成一个view把数据显示出来,并且有修改添加功能.
效果如下:



修改如下:



The ActiveScafold plugin for Rails promises to be a huge time saver.  In just a few easy steps, you can create a full web interface for your database, complete with inline editing and fold out panels.  Of course, it helps to have some grasp about what it is doing or you can get stuck like I did this morning.  I’m no expert (yet), but since it is so very cool, I wanted to share what I’ve learned (with the help of Sean Dick and Ivan Storck at tonight’s SFRuby Hack session).

After installing the plugin, there are just 3 lines of code that magically generate the HTML pages, but the trick is knowing where to put them. There’s a nice intro on the github wiki that outlines common use cases:

  • Prototyping
  • Admin Interfaces
  • Embedded, Widget-Style
  • Data-Heavy Applications

The use case that led me to ActiveScaffold today was the creation of an admin interface.  I’m working on a website and the end user stuff is pretty nice, but there are a bunch of tables where the data needs a little love… no one wants to launch the site without at least a few corrections in the data and it is crazy to either delay the launch while we build an admin interface or have an engineer make corrections with sql updates.  Enter ActiveScaffold: a way to allow admins to make the changes they need with very little software development.  (Later I expect we’ll need to add some fancy bits to the admin interface, but ActiveScaffold promises to be configurable and extensible enough when the time comes and the key point is that I don’t expect to need those features this week.)

ActiveScaffold for Admin
Make a little app for this experiment:

rails active_scaffold
cd active_scaffold
./script/generate scaffold Task title:string notes:text  complete:boolean
rake db:migrate


Install the plugin, which is compatible with Rails 2.3.2 (yay!) and previous versions of rails (if you install  a specific revision)

./script/plugin install git://github.com/activescaffold/active_scaffold.git


Now we have an app that lets you create, view, edit and delete tasks. This is the end-user app, you could edit the views and remove controller actions to prevent editing, deleting and/or creation. We want to leave this interface as is, but create a separate set of pages to allow an administrator to view, create, modify and delete tasks.

Sean came up with the idea of using routes with a namespace to facilitate this. Here’s what we came up with:

In config/routes.rb add the following code:

map.namespace :admin do |admin|
   admin.resources :tasks
end



Create a copy of /app/views/layouts/tasks.html.erb and call it admin.html.erb (in same folder), then add the following lines inside the tag:





Create app/controllers/task_controller.rb:

class Admin::TasksController < TasksController
   layout "admin"
   active_scaffold :task
end



Check it out:

http://localhost:3000/admin/tasks

  • 插件activescaffold 生成数据显示 用法示例_第1张图片
  • 大小: 38.8 KB
  • 插件activescaffold 生成数据显示 用法示例_第2张图片
  • 大小: 56.9 KB
  • 查看图片附件

你可能感兴趣的:(Rails,Git,UP,SQL,IDEA)