在创建控制器和模型之后,仅创建Ruby on Rails视图

Sea*_*ean 41 views ruby-on-rails models scaffold

我已经获得了一个具有控制器(仅限最少代码)和模型的项目,但缺少视图.有没有办法只使用脚手架或其他工具生成视图?

Dav*_*ton 80

rails g scaffold User --migration=false --skip
Run Code Online (Sandbox Code Playgroud)

--skip跳过已存在文件的方法.(相反的是--force.)

如果你不想要帮助者,--helpers=false.

删除我的User观看后的示例输出:

      invoke  active_record
   identical    app/models/user.rb
      invoke    test_unit
   identical      test/unit/user_test.rb
        skip      test/fixtures/users.yml
       route  resources :users
      invoke  scaffold_controller
   identical    app/controllers/users_controller.rb
      invoke    erb
       exist      app/views/users
      create      app/views/users/index.html.erb
      create      app/views/users/edit.html.erb
      create      app/views/users/show.html.erb
      create      app/views/users/new.html.erb
      create      app/views/users/_form.html.erb
      invoke    test_unit
   identical      test/functional/users_controller_test.rb
      invoke    helper
   identical      app/helpers/users_helper.rb
      invoke      test_unit
   identical        test/unit/helpers/users_helper_test.rb
      invoke  assets
      invoke    coffee
   identical      app/assets/javascripts/users.js.coffee
      invoke    scss
   identical      app/assets/stylesheets/users.css.scss
      invoke  scss
   identical    app/assets/stylesheets/scaffolds.css.scss
Run Code Online (Sandbox Code Playgroud)

  • 注意:如果您希望生成的视图具有属性,则需要在模型之后包含它们,例如用户名:字符串电子邮件:字符串等。 (2认同)

Ric*_*ith 15

这是脚手架生成器内部调用的内容:

rails g erb:scaffold User
Run Code Online (Sandbox Code Playgroud)

erb是使用的模板引擎,所以你也可以使用haml:scaffold.

你必须明确指定的字段,你想脚手架使用-轨道并不会自动从创建的模型推导出它们.例如:

rails g erb:scaffold User firstname lastname reputation
Run Code Online (Sandbox Code Playgroud)

请参阅rails g --help跳过,强制覆盖和干运行或generate scaffold --help有关生成脚手架的信息等选项.

  • 我觉得这实际上是正确的答案。所有其他选项都会生成缺少的内容。这仅生成视图。谢谢 :) (2认同)