简体   繁体   English

Rails 生成 controller 命令不构建视图文件

[英]Rails generate controller command not building view files

I have been struggling to understand why I can't get view files created for my Rails project.我一直在努力理解为什么我无法为我的 Rails 项目创建视图文件。 The documentation shows that generating a controller also generates an associated view: Rails Docs文档显示生成 controller 也会生成关联视图: Rails Docs

If I use that same command, I get one file generated, being the controller.如果我使用相同的命令,我会生成一个文件,即 controller。 My command我的命令

The only two files within the Views folder in my Rails project are mailer.html.erb and mailer.text.erb我的 Rails 项目中 Views 文件夹中仅有的两个文件是 mailer.html.erb 和 mailer.text.erb

Am I doing something wrong?难道我做错了什么? Do I manually create the view files, and if so I can't seem to get them "connected" to the associated controller.我是否手动创建视图文件,如果是这样,我似乎无法将它们“连接”到关联的 controller。 I am new to Rails, so any insight would be helpful.我是 Rails 的新手,所以任何见解都会有所帮助。 Thanks!谢谢!

try this command:试试这个命令:

rails g controller Articles index create

You can create methods and views dynamically after If you typed controller name in command.如果在命令中键入 controller 名称,则可以动态创建方法和视图。

you are creating only a controller in your command not any methods that's why the views are not generated.您在命令中仅创建 controller 而不是任何方法,这就是未生成视图的原因。 If you need to generate views for the particular methods then you may run the below listed command.如果您需要为特定方法生成视图,则可以运行下面列出的命令。

rails g controller Articles index show导轨 g controller 文章索引显示

Here, index, show are the methods name.这里,index、show 是方法名。 So, this command will create ArticlesController and also the respective view files.因此,此命令将创建 ArticlesController 以及相应的视图文件。

You can use the scaffold option.您可以使用脚手架选项。

rails g scaffold Post name:string title:string content:text

This command will generate the following files.此命令将生成以下文件。

File文件 Purpose目的
db/migrate/20100207214725_create_posts.rb Migration to create the posts table in your database (your name will include a different timestamp)迁移以在您的数据库中创建帖子表(您的姓名将包含不同的时间戳)
app/models/post.rb The Post model邮政model
test/unit/post_test.rb Unit testing harness for the posts model model 帖子的单元测试工具
test/fixtures/posts.yml Sample posts for use in testing用于测试的示例帖子
config/routes.rb Edited to include routing information for posts编辑以包括帖子的路由信息
app/controllers/posts_controller.rb The Posts controller帖子 controller
app/views/posts/index.html.erb A view to display an index of all posts显示所有帖子索引的视图
app/views/posts/edit.html.erb A view to edit an existing post编辑现有帖子的视图
app/views/posts/show.html.erb A view to display a single post显示单个帖子的视图
app/views/posts/new.html.erb A view to create a new post创建新帖子的视图
app/views/posts/_form.html.erb A partial to control the overall look and feel of the form used in edit and new views用于控制编辑和新视图中使用的表单的整体外观和感觉的部分
test/functional/posts_controller_test.rb Functional testing harness for the posts controller controller 柱的功能测试工具
app/helpers/posts_helper.rb Helper functions to be used from the post views从帖子视图中使用的辅助函数
test/unit/helpers/posts_helper_test.rb Unit testing harness for the posts helper帖子助手的单元测试工具
app/assets/javascripts/posts.js.coffee CoffeeScript for the posts controller CoffeeScript 用于柱子 controller
app/assets/stylesheets/posts.css.scss Cascading style sheet for the posts controller帖子 controller 的级联样式表
app/assets/stylesheets/scaffolds.css.scss Cascading style sheet to make the scaffolded views look better级联样式表以使脚手架视图看起来更好

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

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