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

发布于 2024-12-15 10:10:24 字数 62 浏览 5 评论 0原文

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

I've obtained a project that have controllers (minimal code only) and models, but the views are missing. Is there a way to generate the views only using scaffold or another tool?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

凝望流年 2024-12-22 10:10:25

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

rails g erb:scaffold User

erb 是使用的模板引擎,因此您也可以使用haml:scaffold

您必须明确指定您希望脚手架使用的字段——rails不会自动从创建的模型中推断出它们。例如:

rails g erb:scaffold User firstname lastname reputation

请参阅rails g --help了解跳过、强制覆盖和空运行等选项,或generatescaffold --help了解特定于生成脚手架的信息。

This is what the scaffold generator calls internally:

rails g erb:scaffold User

erb is the templating engine used, so you can also use haml:scaffold.

You must explicitly specify the fields you would like the scaffolding to use--rails does not automatically deduce them from the created model. For example:

rails g erb:scaffold User firstname lastname reputation

See rails g --help for options like skipping, forcing overwriting, and dry runs or generate scaffold --help for information specific to generating scaffolding.

双手揣兜 2024-12-22 10:10:25

我刚刚遇到和你一样的问题。我做到了。更多详细信息如下:
- 首先,我将views/your_model文件夹重命名为views/your_model_bak。为了以后失败时恢复
- 然后,执行命令

rails g scaffold YourModel [field[:type][:index]] --skip
  • 不要忘记 --skip 选项,它不会创建现有文件(本例中为控制器和模型以及其他一些文件)
  • 确保列表 [field[:type][:index]] 是最新的

-- 最后,您应该在 your_model 控制器中更新您的许可证。

希望它可以帮助你。

I just encounter the same your problem. I did it. More detail is below:
- First I rename views/your_model folder to views/your_model_bak. In order to revert if fail later
- Then, execute command

rails g scaffold YourModel [field[:type][:index]] --skip
  • Don't forget --skip option, it will not create exist files (controller and model in this case and few other files)
  • Make sure list [field[:type][:index]] is up to date

-- Finally, you should update your permit in your_model controller.

Hope it can help you.

沫离伤花 2024-12-22 10:10:25

“另一个工具”...

能够执行“script/generate view_for model_name”怎么样? :)

有一个宝石可以做到这一点 - View Mapper。它有 Ruby on Rails 2 和 3 版本。

"Another tool"...

How about being able to do "script/generate view_for model_name"? :)

There is a gem for that - View Mapper. It has Ruby on Rails 2 and 3 versions.

云淡风轻 2024-12-22 10:10:25

一个小技巧是如果使用 Rspec 并且不希望为规范/视图中的每个视图生成测试文件,请添加“--no-test-framework

One small tip is to add "--no-test-framework" if using Rspec and don't want test files generated for each view in spec/views

瞳孔里扚悲伤 2024-12-22 10:10:25

要在创建控制器和模型后生成视图,您可以使用命令行。您切换到要在其中创建新视图的文件夹。例如:

$ cd name_app/app/views/controller_name
$ touch name_file

要返回一个目录,请使用:

$ cd ..

To generate views after controller and models are already created, you may use the command line. You switch to the folder in which you want to create the new view. For example:

$ cd name_app/app/views/controller_name
$ touch name_file

To go back of one directory use:

$ cd ..
不必了 2024-12-22 10:10:24
rails g scaffold User --migration=false --skip

--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
rails g scaffold User --migration=false --skip

The --skip means to skip files that already exist. (The opposite is --force.)

If you don't want helpers, --helpers=false.

Sample output after deleting my User views:

      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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文