Dav*_*ers 27 ruby ruby-on-rails
这是一个微不足道的问题,但我很好奇:
通常的生成器配置是否有办法在运行时关闭视图文件夹和操作模板的创建rails generate controller?
我无法在任何地方找到一个选项,这里的代码没有向我显示任何指针.
无论如何,对于我们的API,我们可能会在某些时候构建我们自己的控制器/资源生成器,但我很好奇是否有办法在此期间关闭这种烦恼.
dim*_*uch 41
它不是一个记录良好的功能,但尝试在命令中添加--skip-template-engine(别名--no-template-engine)选项.
rails generate controller foo bar --skip-template-engine
Run Code Online (Sandbox Code Playgroud)
虚拟应用程序上的演示:
rails g controller my_controller index show --no-template-engine
create app/controllers/my_controller_controller.rb
route get "my_controller/show"
route get "my_controller/index"
invoke test_unit
create test/functional/my_controller_controller_test.rb
invoke helper
create app/helpers/my_controller_helper.rb
invoke test_unit
create test/unit/helpers/my_controller_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/my_controller.js.coffee
invoke scss
create app/assets/stylesheets/my_controller.css.scss
Run Code Online (Sandbox Code Playgroud)
Den*_*nis 26
要跳过使用控制器生成的视图,请禁用模板引擎.
一旦:
rails g controller ControllerName action1 action2 --skip-template-engine
Run Code Online (Sandbox Code Playgroud)
请注意,每个--skip选项也都有一个别名--no选项.
默认:
# config/application.rb
config.generators do |g|
g.template_engine false
end
# OR
config.generators.template_engine = false
Run Code Online (Sandbox Code Playgroud)
如果您有一个仅限API的应用程序(没有前端),您可能还希望跳过使用控制器生成的资产和帮助程序.
一旦:
rails g controller api/users --no-helper --no-assets --no-template-engine
Run Code Online (Sandbox Code Playgroud)
默认:
# config/application.rb
config.generators do |g|
g.assets false
g.helper false
g.template_engine false
end
# OR
config.generators.assets = false
config.generators.helper = false
config.generators.template_engine = false
Run Code Online (Sandbox Code Playgroud)
禁用资产会跳过生成的样式表和 javascripts.如果你只是想跳过一个,使用--no-stylesheets或--no-javascripts,或config/application.rb使用:
config.generators.stylesheets = false
config.generators.javascripts = false
Run Code Online (Sandbox Code Playgroud)
如果您的默认配置会跳过生成的内容(例如资产和帮助程序),但在一种情况下需要它们,您可以像这样生成它们:
rails g controller foo --helper --assets --skip
Run Code Online (Sandbox Code Playgroud)
其中--skip跳过生成已经存在的文件.
只是想我会尝试强调--skip-template-engine标志,看看它是否在发电机中起作用,它起了作用!没有从bin/rails g controllerRails 4.2应用程序中的命令生成视图模板.
尝试:
config.generators do |g|
g.template_engine false
end
Run Code Online (Sandbox Code Playgroud)
我知道有点晚了,但这些东西在谷歌里徘徊!;)
| 归档时间: |
|
| 查看次数: |
15543 次 |
| 最近记录: |