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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8114866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:21:00  来源:igfitidea点击:

Create Ruby on Rails views (only) after controllers and models are already created

ruby-on-railsviewsmodelsscaffold

提问by Sean

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?

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

回答by Dave Newton

rails g scaffold User --migration=false --skip

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

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

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

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

Sample output after deleting my Userviews:

删除我的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

回答by Rick Smith

This is what the scaffold generator calls internally:

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

rails g erb:scaffold User

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

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

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

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

rails g erb:scaffold User firstname lastname reputation

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

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

回答by Michael Durrant

"Another tool"...

“另一个工具”...

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

能做“ script/generate view_for model_name”怎么样?:)

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

有一个宝石 - View Mapper。它有 Ruby on Rails 2 和 3 版本。

回答by Ken Block

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

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

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
  • 不要忘记 --skip 选项,它不会创建现有文件(在这种情况下是控制器和模型以及其他一些文件)
  • 确保列表 [field[:type][:index]] 是最新的

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

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

Hope it can help you.

希望它可以帮助你。

回答by Dave Burke

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

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

回答by Leonardo Barazza

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 ..