简体   繁体   English

为什么“g controller”也没有创建视图文件?

[英]Why doesn't “g controller” also create view file?

I've seen some Rails examples where generating a controller also creates the view file (not just the view folder). 我已经看到一些Rails示例,其中生成控制器也会创建视图文件(而不仅仅是视图文件夹)。

In section 6.4 of Rails Guide , it shows an empty view folder and no view file. Rails指南的 6.4节中,它显示了一个空视图文件夹,没有视图文件。 This is what my local installation is doing. 这是我的本地安装正在做的事情。 I don't get any view files. 我没有得到任何视图文件。

Is there some way to have Rails auto generate the view file when running rails g controller ... ? 有没有办法让Rails在运行rails g controller ...时自动生成视图文件rails g controller ... Or, is it more likely the person created the view file manually and didn't show that part? 或者,这个人是否更有可能手动创建视图文件并且没有显示该部分?

Use rails generate scaffold instead which will generate the model, view and controller files for you in a single operation. 使用rails生成脚手架 ,它将在一次操作中为您生成模型,视图和控制器文件。

If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job. 如果要在单个操作中为新资源创建模型,视图和控制器,则脚手架是工作的工具。

eg: 例如:

rails g scaffold Post name:string title:string content:text

But, if you really want to use rails g controller and also create the view files for you, then you have to specify the action names for your controller: 但是,如果您真的想使用rails g controller并为您创建视图文件,则必须为控制器指定action名称:

rails g controller Controllername index show edit update

This way, it will create the view files for these four actions: app/views/.../index.html.erb , app/views/.../edit.html.erb . 这样,它将为这四个操作创建视图文件: app/views/.../index.html.erb , app/views/.../edit.html.erb . . etc. 等等

But, looking at your case, you should use scaffolding as it will do a lot of work for you. 但是,看看你的情况,你应该使用脚手架,因为它会为你做很多工作。

To generate basic view and controller actions you should run eg: rails g controller Controllername index show 要生成基本视图和控制器操作,您应该运行例如: rails g controller Controllername index show

Basic view for index and show action will be created. 将创建索引和显示操作的基本视图。

I've got the same problem if i create an api-project with RubyMine (New Project > Rails Api Project). 如果我用RubyMine(New Project> Rails Api Project)创建一个api项目,我也会遇到同样的问题。 The first project was created on cli (rails new), ze second in RM: 第一个项目是在cli(rails new)上创建的,在RM中是第二个:

C:\Projekte\railstest3>ruby bin/rails generate controller Welcome index
      create  app/controllers/welcome_controller.rb
       route  get 'welcome/index'
      invoke  erb
      create    app/views/welcome
      create    app/views/welcome/index.html.erb
      invoke  test_unit
      create    test/controllers/welcome_controller_test.rb
      invoke  helper
      create    app/helpers/welcome_helper.rb
      invoke    test_unit
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/welcome.coffee
      invoke    scss
      create      app/assets/stylesheets/welcome.scss

C:\Projekte\railstest3>cd..

C:\Projekte>cd railstest

C:\Projekte\railstest>ruby bin/rails generate controller Welcome4 index
      create  app/controllers/welcome4_controller.rb
       route  get 'welcome4/index'
      invoke  test_unit
      create    test/controllers/welcome4_controller_test.rb

C:\Projekte\railstest>rails -v
Rails 5.0.2

Solution is to create new "Application Projects" and not new "Api Project" in RM. 解决方案是在RM中创建新的“应用项目”而不是新的“Api项目”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM