Posted by cnruby Tue, 31 Jul 2007 20:02:00 GMT

Rails框架技术讲座:数据库后台管理插件RailsMyAdmin

目录
 (一)系统环境:

  1. Ruby 语言 1.8.4版本, 点击这里Ruby1.8.4。要想安装多个Ruby语言运行环境请看这里
  2. Rails 框架 1.2.1版本,安装方法请看这里,最简单方法是第一种方法即可。
  3. Windows XP 或者 Windows 2000操作系统 或者 Linux操作系统
  4. 需要一个浏览器,如FireFox1.5.0.1以上版本。
  5. 开发编辑工具 Notepad2 ,安装方法请单击这里,复制一个notepad2.exe,并且更名为vi.exe。
  6. 在Windows XP上安装Linux核心命令,点击这里
  7. 如何在Windows Console下使用命令svn(下载软件),点击这里

 (二)前提条件:

  1. 在本机Winodw操作系统上,我们的工作目录为d:\works_rails。
  2. 你的电脑必须在线。

 (三)目的:

  1.  RailsMyAdmin是一个Rails框架软件的插件,它可以管理你的Rails框架软件数据库,注意不仅仅可以是MySQL数据库,也可以是其它的数据库。
  2. 本讲座的完整代码请您在Google Code Hosting上查看:
    http://cnruby.googlecode.com/svn/trunk/rails-projects/use_railsmyadmin
    或者下载
    svn co http://cnruby.googlecode.com/svn/trunk/rails-projects/use_railsmyadmin

 (四)解决方案:

  1. 创建一个Rails框架的应用软件,切换到软件的根目录。
    rails use_railsmyadmin
    cd use_railsmyadmin
  2. 修改数据库配置文件 database.yml :
    vi config/database.yml
    【具体内容如下:用下面的代码替换原文件的所有内容】
    development:
      adapter: mysql
      database: myadmin
      username: root
      password: root
      host: localhost

    test:
      development

    production:
      development
  3. 创建一个MySQL数据库myadmin
    mysqladmin -u root -proot create myadmin
  4. 自动生成一个基于模型Admin的相关代码,增加表admins的字段username和password,最后执行生成数据库表的命令。
    ruby script/generate scaffold_resource admin

    vi db/migrate/001_create_admins.rb
    【具体内容如下:放在create_table一行下面】
          t.column :username, :string
          t.column :password, :string

    rake db:migrate
  5. 第一步:安装RailsMyAdmin插件;第二步:生成应用于我们软件的代码;第三步:修改我们软件的环境配置文件。
    ruby script/plugin install http://railsmyadmin.googlecode.com/svn/trunk/my_admin/

    要是第一次安装失败,再使用下面命令执行。
    ruby script/plugin install --force http://railsmyadmin.googlecode.com/svn/trunk/my_admin/

    ruby script/generate my_admin

    vi config/environment.rb
    【具体内容如下:放在该文件最后】
    ## MY ADMIN CONFIG  BEGIN
    require 'my_admin/my_admin_tool'

    # If you only want certain models to be available to RailsMyAdmin,
    # set :all_models to false and specify the desired models in MY_ADMIN_MODELS
    MY_ADMIN_GLOBALS  = {:all_models => true, :confirm_destroy => false}

    # Uncomment the following line if you set :all_models to false above.
    #MY_ADMIN_MODELS = [User, Content]
    # Replace [User, Content] with your desired array of model classes that
    # RailsMyAdmin should be restricted to.

    # MY_ADMIN_AUTH must define a Proc object that takes as a paramater
    #   an ApplicationController instance variable (c - in the example below).
    # If you have a method defined in your ApplicationController,
    #   'admin_logged_in?' for example, the following sample code will
    #    authenticate against that method and only allow visitors to
    #    view RailsMyAdmin if the 'admin_logged_in?' method returns true.

    MY_ADMIN_AUTH     = Proc.new { |c| c.send('admin_logged_in?') }
    ## MY ADMIN CONFIG  END
  6. 启动网络服务器
    ruby script/server
  7. 再打开一个Shell,执行下面命令
    start http://localhost:3000/my_admin/main
  8. 我们看到该插件结果图:

 (五)视听教学:




 (六)必须注意的问题:




 (七)参考资料:




 (八)命令清单:

rails use_railsmyadmin
cd use_railsmyadmin

vi config/database.yml
development:
adapter: mysql
database: myadmin
username: root
password: root
host: localhost

test:
development

production:
development

mysqladmin -u root -proot create myadmin

ruby script/generate scaffold_resource admin
vi db/migrate/001_create_admins.rb
t.column :username, :string
t.column :password, :string
rake db:migrate

ruby script/plugin install http://railsmyadmin.googlecode.com/svn/trunk/my_admin/
ruby script/plugin install --force http://railsmyadmin.googlecode.com/svn/trunk/my_admin/
ruby script/generate my_admin
vi config/environment.rb
## MY ADMIN CONFIG BEGIN
require 'my_admin/my_admin_tool'

# If you only want certain models to be available to RailsMyAdmin,
# set :all_models to false and specify the desired models in MY_ADMIN_MODELS
MY_ADMIN_GLOBALS = {:all_models => true, :confirm_destroy => false}

# Uncomment the following line if you set :all_models to false above.
#MY_ADMIN_MODELS = [User, Content]
# Replace [User, Content] with your desired array of model classes that
# RailsMyAdmin should be restricted to.

# MY_ADMIN_AUTH must define a Proc object that takes as a paramater
# an ApplicationController instance variable (c - in the example below).
# If you have a method defined in your ApplicationController,
# 'admin_logged_in?' for example, the following sample code will
# authenticate against that method and only allow visitors to
# view RailsMyAdmin if the 'admin_logged_in?' method returns true.

MY_ADMIN_AUTH = Proc.new { |c| c.send('admin_logged_in?') }
## MY ADMIN CONFIG END

ruby script/server
start http://localhost:3000/my_admin/main


 (九)下载文件pdf:




________________________________________________________________ 您只要点击下面图标,就可以把本文加入到您喜欢的公共收藏库中去。
del.icio.us Digg | FURL | Yahoo! My Web 2.0 | Reddit | Blinklist | Fark

Posted in  | Tags , ,