[英]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![]() |
test/unit/post_test.rb |
Unit testing harness for the posts 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![]() |
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 ![]() |
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 ![]() |
app/assets/stylesheets/posts.css.scss |
Cascading style sheet for the posts 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.