为什么脚手架在 Ruby on Rails 中不起作用?
我创建了一个控制器和一个模型。控制器称为“Admin”,模型称为“Album”。我使用正确的信息编辑了database.yml,并执行了rake db:migrate命令,该命令没有返回任何错误,并且确实迁移了schema.rb内的数据库。在控制器内我写道:
class AdminController < ApplicationController
scaffold :album
end
接下来我启动了服务器并转到 http://localhost:3000/admin 但是我没有看到典型的 CRUD 页面,而是收到以下错误:
app/controllers/admin_controller.rb:3 要求 参数: 没有任何 显示会话转储 --- 闪存: !map:ActionController::Flash::FlashHash
{}
<前><代码>响应 标题: {“cookie”=>[], “缓存控制”=>“无缓存”}
知道为什么吗?
I created a controller and a model. The controller is called "Admin" and the model is called "Album". I edited database.yml with proper info and did the rake db:migrate command which didn't return any errors and did migrate the db inside schema.rb. Inside the controller I wrote:
class AdminController < ApplicationController
scaffold :album
end
Next I started my server and went to http://localhost:3000/admin but instead of seeing the typical CRUD page I get the following error:
app/controllers/admin_controller.rb:3 Request Parameters: None Show session dump --- flash: !map:ActionController::Flash::FlashHash
{}
Response Headers: {"cookie"=>[], "Cache-Control"=>"no-cache"}
Any idea why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
脚手架的语法已被弃用相当长一段时间了。如今,rails(版本 2.x)使用以下方法来搭建资源:
生成脚手架视图(在 app/views 中)、控制器(app/controllers)、标准测试(在 test/ 中),最重要的是,使脚手架工作所需的路线。
我相信 Rails 开发团队取消了旧的语法(“脚手架:资源”),因为没有真正的应用程序会让脚手架保持不变,即。你总是需要某种定制。使用新语法,您可以保持不变,但自定义也更容易。
如果您确实需要将控制器命名为 admins,则可以在生成脚手架后更改文件 config/routes.rb。但这毫无意义:为什么创建新相册的 URI 应该称为“/admins/new”?
如果您尝试为相册应用程序创建管理区域,您可能正在寻找命名空间(这样您就可以在“admin”命名空间内拥有多个不同的资源、控制器和视图)。要在管理命名空间中创建相册资源,请编写:
在这种情况下,您的控制器将可以通过 http://host/ 访问管理/相册。
That syntax for scaffolding has been deprecated for quite some time. Nowadays, rails (versions 2.x) use the following method to scaffold a resource:
That generates the scaffolding views (in app/views), the controller (app/controllers), standard tests (in test/) and, crucially, the required routes to make scaffolding work.
I believe the rails dev team took away the old syntax ("scaffold :resource") because no real application would ever leave a scaffold untouched, ie. you will always need some kind of customization. With the new syntax you can leave it untouched, but it is also much easier to customize.
If you really need your controller to be named admins, you can change the file config/routes.rb after generating the scaffolding. It makes no sense, though: Why should the URI to create a new album be called "/admins/new"?
If you are trying to create an admin area for an image album app, you are probably looking for namespaces (so you can have multiple different resources, controllers and views inside the "admin" namespace). To create an album resource within the admin namespace, write:
In that case, your controller will be accessible as http://host/admin/albums.
嗯,
通常你会有一个控制器和一个名为 Admin 的模型,专辑也是如此,
看看这个快速屏幕截图,博客是如何使用脚手架完成的;
创建网络博客
Hm,
Normally you would have a controller and a model called Admin and the same thing would be about Album,
Take a look at this quick screen cast how a blog is done using scaffolding;
Creating a web-blog
script/generate 命令似乎不起作用,有人必须提供 ./script/generate ,我认为这是一个 linux 目录问题,你必须明确地说你是从当前目录(./)开始的。希望这可以帮助某人避免挠头
the script/generate command seems not to work, someone has to provide ./script/generate , I think its a linux directory issue, you have to explicitly say you are starting from the current directory (./). hope this helps someone avoid scratching his head