简体   繁体   English

Ruby 在 Rails 上没有为 Controller 创建视图

[英]Ruby On Rails Not Creating The View For A Controller

I'm definitely very new to Ruby on Rails.我绝对是 Ruby on Rails 的新手。 I created a new rails folder like this:我创建了一个新的 rails 文件夹,如下所示:

Edit: Sorry for the inconvienience, but I did use the --api tag.编辑:很抱歉给您带来不便,但我确实使用了--api标签。

rails new <project-name> -d mysql --api

since I use mysql instead of sqlite .因为我使用mysql而不是sqlite Then, in the tutorial they used a command like this:然后,在教程中,他们使用了这样的命令:

rails generate controller Welcome index

I did the exact same thing, however in the log it shows this:我做了完全相同的事情,但是在日志中它显示了这一点:

   create  app/controllers/welcome_controller.rb
   route  get 'welcome/index'

It didn't seem to create the view.它似乎没有创建视图。 I don't know why it's doing this.我不知道它为什么这样做。 After I start the server with rails server I can go to the localhost:3000/welcome/index but it just takes me back to the home page, and in the log it says Started GET.... No Content... .在我使用rails server启动服务器后,我可以 go 到localhost:3000/welcome/index但它只是将我带回主页,并在日志中显示Started GET.... No Content... So it couldn't find the view.所以它找不到视图。 How can I fix this?我怎样才能解决这个问题?

Unless you created an API only project with the --api flag, the rails generate controller Welcome index command should have created an index.html.erb file in the views folder.除非您使用--api标志创建了仅 API 项目,否则rails generate controller Welcome index命令应该在视图文件夹中创建了index.html.erb文件。

You can either delete the welcome_controller.rb file and the generated line from the route.rb file and run the command again or you can manually create the view with the touch command (assuming your OS supports the touch command):您可以删除welcome_controller.rb文件和route.rb文件中生成的行并再次运行该命令,或者您可以使用touch命令手动创建视图(假设您的操作系统支持touch命令):

touch app/views/welcome/index.html.erb

or through you favourite text editor or IDE.或通过您喜欢的文本编辑器或 IDE。 On a side note, controller names should be plural ( WelcomePagesController for example).附带说明,controller 名称应该是复数(例如WelcomePagesController )。

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

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