The Last Day Of Summer

.NET技术 C# ASP.net ActiveReport SICP 代码生成 报表应用 RDLC
posts - 305, comments - 1973, trackbacks - 78, articles - 3
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理

在上一篇Ruby on rails开发来(windows)(三)-实现页面间的跳转中,我们创建了两个页面来进行跳转迁移,这次我们来写一个单表维护的添删查改的例子。

 

1.       这次我们重新创建一个项目depot,按照上篇中的步骤,创建depot项目。

2.       创建数据库。

你可以使用rails的命令行,通过mysql创建,先定位到depot目录,使用命令:

depot> mysql -u root –p

 

密码为空,连接mysql后执行下面的命令:

mysql> create database depot_development;

mysql> create database depot_test;

mysql> create database depot_production;

mysql> grant all on depot_development.* to 'dave'@'localhost';

mysql> grant all on depot_test.* to 'dave'@'localhost';

mysql> grant all on depot_production.* to 'prod'@'localhost' identified by 'wibble';

mysql> exit

创建数据库完成后,修改depot\config目录下的database.yml文件的内容,将其中development库和test库的username项设置为空。

 

你也可以用InstantRails中自带的phpmyadmin来创建,PhpMyAdmin的启动可以按照下图:

 

在这里我们要创建三个数据库depot_development,depot_test,depot_public,这三个库分别用于开发,测试,发布。

3.       depot项目的db目录下创建一个create.sql文件,内容为:

drop table if exists products;

create table products (

id int not null auto_increment,

title varchar(100) not null,

description text not null,

image_url varchar(200) not null,

price decimal(10,2) not null,

date_available datetime not null,

primary key (id)

);

4.       使用PhpMyAdmin,选择depot_development库,导入上面的脚本,创建Product表。完成后可以看到下图

   

5.        现在万事具备,只欠东风了,下面运行Rails的命令行,如下图:

      

定位到depot目录,执行命令:

ruby script/generate scaffold product Admin,回车,会在命令行窗口输出:

      exists  app/controllers/

      exists  app/helpers/

      exists  app/views/admin

      exists  app/views/layouts/

      exists  test/functional/

  dependency  model

      exists    app/models/

      exists    test/unit/

      exists    test/fixtures/

   identical    app/models/product.rb

   identical    test/unit/product_test.rb

   identical    test/fixtures/products.yml

overwrite app/views/admin/_form.rhtml? [Ynaqd] a (输出到这里的时候会停一下,输入“a”继续,输出如下)

forcing scaffold

       force  app/views/admin/_form.rhtml

   identical  app/views/admin/list.rhtml

   identical  app/views/admin/show.rhtml

   identical  app/views/admin/new.rhtml

   identical  app/views/admin/edit.rhtml

   identical  app/controllers/admin_controller.rb

   identical  test/functional/admin_controller_test.rb

   identical  app/helpers/admin_helper.rb

   identical  app/views/layouts/admin.rhtml

   identical  public/stylesheets/scaffold.css

 

6.        至此,我们的编码工作就完成了,现在就是试试看你的页面了,在浏览器地址栏中输入:http://127.0.0.1/Admin/New,看到了什么?一个Product表的维护界面,如图:

 

填入内容后点击Create,会退回到list页面,我们看到Rails把列表,编辑,删除和分页都生成好了,如图:

      

OK,这次就到这里,以前只是听说Rails开发效率如何如何高,现在亲身体验下感觉确实很方便,不知深入下去以后会我的观点会有什么变化。

Feedback

#1楼   回复  引用    

2007-06-22 17:36 by Ruby中国[未注册用户]
:)

#2楼   回复  引用    

2007-07-26 17:15 by welkin[未注册用户]
你可以使用rails的命令行,通过mysql创建,先定位到depot目录,使用命令:

depot> mysql -u root –p

创建数据库没有成功呀,这个命令对吗?

#3楼   回复  引用    

2007-08-17 15:23 by wch[未注册用户]
我按照步骤都作了,但是运行 http://127.0.0.1/Admin/New时打不开网页,前辈们知道为什么吗?

#4楼   回复  引用    

2007-10-10 13:37 by CDmyname[未注册用户]
1 确保是按步骤来的
2 确保是在项目目录下 ruby script/server 了
3 确保你是人类
4 保证我是正确的
5 确保上帝没有和你开玩笑

#5楼   回复  引用    

2007-12-10 16:25 by ericazhangjun[未注册用户]
应该将link http://127.0.0.1/Admin/New 改为:http://127.0.0.1:3000/Admin/New

#6楼[楼主]   回复  引用  查看    

2007-12-10 20:29 by Cure      
@ericazhangjun
谢谢指出问题。

#7楼   回复  引用  查看    

2008-01-05 02:54 by 辉郎      
创建数据表脚本创建的是Products
而后来运行的命令行:
ruby script/generate scaffold product Admin
你确定没有问题吗?我这里执行这个命令行总是报一下错误:
wrong number of arguments (1 for 2)

#8楼   回复  引用    

2008-01-07 23:55 by 六翼[未注册用户]
同楼上,好像是参数错误的意思,应该1个,实际给了2个?

#9楼[楼主]   回复  引用  查看    

2008-01-08 11:19 by Cure      
@辉郎
@六翼
我这里又运行了一遍脚本,没有问题,是不是版本的问题?
我是在InstantRails1.7版本下作练习的。

#10楼   回复  引用    

2008-01-14 12:30 by be0701[未注册用户]
C:\InstantRails-2.0-win\rails_apps\depot2>ruby script/generate scaffold produts,

wrong constant name Produts,sController

这个提示是找不到控制器吗? 如何解决?
当输入两个参数的时候会出现上面 辉郎
所提的错误
前辈 请教

#11楼   回复  引用    

2008-01-14 12:32 by be0701[未注册用户]
补充 我用的是:InstantRails-2.0

#12楼[楼主]   回复  引用  查看    

2008-01-14 12:42 by Cure      
@be0701
你改为product试试?

#13楼   回复  引用    

2008-01-14 13:10 by be0701[未注册用户]
C:\InstantRails-2.0-win\rails_apps\depot2>ruby script/generate scaffold product
exists app/models/
exists app/controllers/
。。。
exists app/views/layouts/
exists test/functional/
exists test/unit/
overwrite app/views/products/index.html.erb? (enter "h" for help) [Ynaqdh] a
forcing scaffold
force app/views/products/index.html.erb
。。。。。。
create app/views/layouts/products.html.erb
create public/stylesheets/scaffold.css
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/product.rb
create test/unit/product_test.rb
create test/fixtures/products.yml
create db/migrate
create db/migrate/001_create_products.rb
create app/controllers/products_controller.rb
create test/functional/products_controller_test.rb
create app/helpers/products_helper.rb
route map.resources :products

然后在访问地址上是:http://127.0.0.1:3000/Admin/New
出现:
No route matches "/Admin/New" with {:method=>:get}

#14楼   回复  引用    

2008-01-14 13:12 by be0701[未注册用户]
Cure 我的qq是:273668867 能请教一下吗?

#15楼[楼主]   回复  引用  查看    

2008-01-14 20:32 by Cure      
@be0701
用不了QQ,给封了
是地址不对吗?
试试
http://127.0.0.1/Admin/New" target="_new">http://127.0.0.1/Admin/New
另外看下你的controller里的URL对应的Action有没有生成?

#16楼   回复  引用    

2008-01-15 11:49 by be0701[未注册用户]
好的 感谢您 百忙中帮我们这些菜鸟们答疑 。。我是刚刚接触rails 好兴奋 呵呵

#17楼   回复  引用    

2008-01-15 13:03 by be0701[未注册用户]
在创建控制器的时候 并没有生成view目录下的 rhtml文件 需要手动来编辑这些文件吗?

#18楼[楼主]   回复  引用  查看    

2008-01-15 14:12 by Cure      
@be0701
随笔里生成的时候有出现这些提示:
identical app/views/admin/list.rhtml

identical app/views/admin/show.rhtml

identical app/views/admin/new.rhtml

identical app/views/admin/edit.rhtml

identical app/controllers/admin_controller.rb

identical test/functional/admin_controller_test.rb

identical app/helpers/admin_helper.rb

identical app/views/layouts/admin.rhtml

identical public/stylesheets/scaffold.css

这些就表明app/views/admin目录下的rhtml文件都已经生成了,看了你运行后的提示,没有生成rhtml文件,试试看看model,数据库表,库的名字是不是规范统一的。

#19楼   回复  引用    

2008-01-15 15:02 by be0701[未注册用户]
数据表的建立是按照上面的步骤粘帖过去 表明为products
controller下的文件名字为products_controller.rb
model文件夹下的文件是product.rb
是因为数据表用了products 产生不一致吗?

#20楼[楼主]   回复  引用  查看    

2008-01-15 16:11 by Cure      
@be0701
我想我发现问题所在了,

我使用的命令:
ruby script/generate scaffold product Admin←少了这个

你使用的命令:
ruby script/generate scaffold product

所以你生成的controller是products_controller,而我生成的是admin_controller,之后你访问http://127.0.0.1:3000/Admin/New" target="_new">http://127.0.0.1:3000/Admin/New 当然就会出错了。

#21楼   回复  引用    

2008-01-15 18:26 by ror[未注册用户]
1.x script/generate scaffold ModelName [ControllerName] [action, ...]
2.x script/generate scaffold ModelName [field:type, field:type]


--引用--------------------------------------------------
Cure: @be0701
我想我发现问题所在了,

我使用的命令:
ruby script/generate scaffold product Admin←少了这个

你使用的命令:
ruby script/generate scaffold product

所以你生成的controller是products_controller,而我生成的是admin_controller,之后你访问<a href="http://127.0.0.1:3000/Admin/New" target="_new">http://127.0.0.1:3000/Admin/New</a> 当然就会出错了。
--------------------------------------------------------

#22楼[楼主]   回复  引用  查看    

2008-01-15 19:06 by Cure      
@ror
谢谢,刚上网查了,确实Rails 2.x里已经改变了

#23楼   回复  引用    

2008-01-16 01:29 by be0701[未注册用户]
原来在rails的命名中 默认数据表通常是复数,而model则需要命名同一单词的单数且首字母大写 然后就可以实现直接绑定 呵呵 方便。

#24楼   回复  引用    

2008-01-16 01:36 by be0701[未注册用户]
补充: 在应用一些复数形式不规则的单词时(如people 单复数同型) model的名字为 People 但是数据表的名则应该是people 而不是常规的peoples 。。
看书看来的。。。大家鉴定一下 怕说错了 呵呵

#25楼   回复  引用    

2008-01-16 13:51 by be0701[未注册用户]
虽然大概知道怎么做了 但是还是没有成功
无奈之下 换了1.7.x的版本的InstantRails来做 哈哈 终于成功了 。。
高手能够说一下 1.7.x与2.0.x的InstantRails的区别吗?

#26楼[楼主]   回复  引用  查看    

2008-01-16 18:31 by Cure      
@be0701
可以看看InstantRails 2.0的release note:
http://instantrails.rubyforge.org/wiki/wiki.pl?Release_Notes_For_Instant_Rails_2.0" target="_new">http://instantrails.rubyforge.org/wiki/wiki.pl?Release_Notes_For_Instant_Rails_2.0

但是Instantrails是集成其他的组件,所以归根结底,问题还是在Rails2.0的变化上。

#27楼   回复  引用    

2008-01-19 16:56 by 六翼[未注册用户]
哈,换了1.7,终于可以打开New product那页了……
而且
++++++++++++++++++++++++
overwrite app/views/admin/_form.rhtml? [Ynaqd] a (输出到这里的时候会停一下,输入“a”继续,输出如下)
-------------------------------
这句也没出现,不管了,可以继续学习就是好:)

#28楼   回复  引用    

2008-02-06 23:18 by michaelym[未注册用户]
如果用InstantRails 2.0的话,好像不能用scaffold直接生成,请LZ能不能用2.0做上述实例,谢谢

#29楼[楼主]   回复  引用  查看    

2008-02-13 09:10 by Cure      
@michaelym
是的,在Rails 2.0里,scaffold命令已经发生了变化,后面我会找时间写Rails2.0的。

#30楼   回复  引用    

2008-05-21 11:36 by dior[未注册用户]
输入ruby script/generate scaffold product Admin回车后就没反应发了 怎么弄都没反应 怎么回事

#31楼[楼主]   回复  引用  查看    

2008-05-21 14:24 by Cure      
@dior
注意看下上面大家的留言,有没有和你相似的问题。
你是用的InstantRails2.0吗,scaffold 命令有变化,不过自己还没有时间研究2.0的内容。

#32楼   回复  引用    

2008-11-06 18:28 by xiongj[未注册用户]
你好请问下中文显示 为什么是乱码?

#33楼   回复  引用    

2008-11-11 14:51 by 爱[未注册用户]
我输入ruby script/generate scaffold product Admin
报错wrong number of arguments (1 for 2)
我是InstantRails2.0的



发表评论

昵称: [登录] [注册]

主页:

邮箱:(仅博主可见)

评论内容:

  登录  注册

[使用Ctrl+Enter键快速提交评论]

0 712083




相关文章:

相关链接: