Rails中新建scaffold、model的区别
最近在看《Agile Web Development with Rails》的中文版,在第10章出现一个问题,不知道大家有否遇到过
书上介绍使用“ruby script/generate model...”来创建模型类 order 和 line_item,
(而在其他地方则是使用“ ruby script/generate scaffold..."来创建模型类
从语法含义上来说,二者到底有什么区别呢?
我只知道在rails代码中,比如 ./app/views 中是不会新建该模型类对应的文件夹,此外在./app/helpers,./app/controllers中也不会有相对应的文件,那么是不是说明使用“model”新建的模型类是一个抽象类呢?【感谢哪位解释】
非常蹊跷的是,在这本书的源代码中,后来我还是找到与 order,line_item模型类相对应的文件,非常齐备哦,views,helpers,controllers中的文件都和 用scaffold创建的模型类一样多
会不会是中文版翻译有错呢,不禁想问?(因为我看的是中文版)
但也不至于order,line_item两个类都出错吧
感谢回答/指教
[解决办法]
this is old rails edition.
maybe you can use new rails version.
$rails g scaffold name
when you run scaffold command, rails will create
migrate, model,controller, helper, view, test and assets(css & js) file.
in the controller file, it will create some actions and related view automatically.
e.g. index, show, new, edit, create, update and delete actions.
it will change config/routes.rb and add 'resources :names' in this file.
hence when you request the server, route will direct to the correct action.
if you run model command, like
$rails g model name
this command will create migrate, model and test/unit file.
there isn't controller and other files.
the difference is:
if you want to create the model, controller and view for a new resource, scaffold is the tool for this job.
$model just create models dynamically.
[解决办法]
scaffold是一个脚本,可以直接创建model、controller、views等相关的文件,直接访问。model只是创建一个model,即继承activerecord的一个子类。