我在我的testapp项目中使用了多个脚手架。
我已经创建了第一个脚手架:
rails g Post title desc:text
它成功并创建了所有相关文件和控制器。
但是当我做了另一个脚手架时:
testapp$ rails g scaffold product name:string information:text 'price:decimal{7,2}' stock:integer available:boolean
invoke active_record
create db/migrate/20140513062549_create_products.rb
create app/models/product.rb
invoke test_unit
create test/models/product_test.rb
create test/fixtures/products.yml
invoke resource_route
route resources :products
invoke inherited_resources_controller
create app/controllers/products_controller.rb
invoke erb
create app/views/products
create app/views/products/index.html.erb
create app/views/products/edit.html.erb
create app/views/products/show.html.erb
create app/views/products/new.html.erb
create app/views/products/_form.html.erb
invoke test_unit
create test/controllers/products_controller_test.rb
invoke helper
create app/helpers/products_helper.rb
invoke test_unit
create test/helpers/products_helper_test.rb
invoke jbuilder
create app/views/products/index.json.jbuilder
create app/views/products/show.json.jbuilder
invoke assets
invoke coffee
create app/assets/javascripts/products.js.coffee
invoke scss
create app/assets/stylesheets/products.css.scss
invoke scss
identical app/assets/stylesheets/scaffolds.css.scss
但是当我打开控制器app/controllers/products_controller.rb
时它是空的,为什么呢?
我还使用activeadmin,我知道它与它无关。
答案 0 :(得分:1)
我无法在基本的Rails安装上重现这一点:
$ rails new empty && cd empty
$ rails g scaffold Post title desc:text
$ rails g scaffold product name:string information:text 'price:decimal{7,2}' stock:integer available:boolean
在此之后,app/controllers/products_controller.rb
拥有我期望它拥有的所有行:
$ wc -l app/controllers/products_controller.rb
74 app/controllers/products_controller.rb
那就是说,如果我将ActiveAdmin添加到Gemfile:
$ echo 'gem "activeadmin", github: "gregbell/active_admin"' >> Gemfile && bundle
并生成脚手架:
$ rails g scaffold product name:string information:text 'price:decimal{7,2}' stock:integer available:boolean
我的app/controllers/products_controller.rb
包含的代码少得多。它不是空的,而是几乎:
class ProductsController < InheritedResources::Base
end
如果这也是您所看到的,那么您的问题来自于ActiveAdmin使用inherited_resources来完成一些繁重的工作。而且你最终得到的是InheritedResource控制器。
但是控制器应该可以正常工作。