简体   繁体   English

Ruby 在轨道上。生成 controller

[英]Ruby on rails.Generating controller

Why when I write rails generate controller home index the index.html.erb file is not created in the views folder?为什么当我写rails generate controller home index时 index.html.erb 文件没有在views文件夹中创建? And when I run rails server and want to watch http://127.0.0.1:3000/home/index terminal output.当我运行rails server并想观看http://127.0.0.1:3000/home/index终端 output 时。

Started GET "/home/index" for 127.0.0.1 at 2021-01-10 01:54:30 +0200 Processing by HomeController#index as HTML Completed 204 No Content in 0ms (ActiveRecord: 0.0ms | Allocations: 60)在 2021-01-10 01:54:30 +0200 HomeController#index as HTML 开始为 127.0.0.1 获取“/home/index” 已完成 204 0 毫秒内无内容(ActiveRecord:0.0 毫秒 | 分配:60)

When I check error log I see HomeController#index , I would like you to check rails routes first.当我检查错误日志时,我看到HomeController#index ,我希望您先检查rails routes You should see a result similar to this:您应该会看到与此类似的结果:

Prefix      Verb   URI Pattern            Controller#Action

home_index  GET    /home/index(.:format)  home#index
...

If you are seeing homecontroller#index instead of home#index you can access by going to related URI pattern.如果您看到的是homecontroller#index而不是home#index ,您可以通过转到相关的 URI 模式来访问。

You can control which files are created when you run rails generate command.您可以控制在运行rails generate命令时创建哪些文件。 This is a sample output:这是一个示例 output:

➜ rails g controller home index    
Running via Spring preloader in process 1934
      create  app/controllers/home_controller.rb
       route  get 'home/index'
      invoke  erb
      create    app/views/home
      create    app/views/home/index.html.erb
      invoke  helper
      create    app/helpers/home_helper.rb
      invoke  assets
      invoke    scss
      create      app/assets/stylesheets/home.scss

As you see there is a file app/views/home/index.html.erb make sure that it is not empty.如您所见,有一个文件app/views/home/index.html.erb确保它不为空。 For example put <p>Home</p> in this file, if it is empty.例如把<p>Home</p>放在这个文件中,如果它是空的。

I hope it is clear enough and I think this will solve your problem.我希望它足够清楚,我认为这将解决您的问题。

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

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