rails 单模型多态
As a beginner of Rails, I often get confused about what generator generates what and the syntax of these commands. Below is a cheat sheet on the four main generators I find most useful.
作为Rails的初学者,我经常对生成器生成什么内容以及这些命令的语法感到困惑。 以下是我发现最有用的四个主要生成器的备忘单。
They build out standard features of certain parts of your rails application. To start any generator type rails g or rails generator followed by your command.
它们构建了Rails应用程序某些部分的标准功能。 要启动任何生成器,请输入rails g或rails generator然后输入命令。
To find a list of all generators you can also type rails g. *make sure you are in your rails app*
要查找所有发电机的列表,您还可以输入rails g 。 *确保您在Rails应用中*
You will see a terminal output that resembles below. This article will focus on the bolded generators: controller, migration, model, and resource.
您将看到类似于以下的终端输出。 本文将重点介绍粗体生成器:控制器,迁移,模型和资源。
General options: -h, [--help] # Print generator's options and usage -p, [--pretend] # Run but do not make any changes -f, [--force] # Overwrite files that already exist -s, [--skip] # Skip files that already exist -q, [--quiet] # Suppress status outputPlease choose a generator below.Rails: application_record assets channelcontroller generator helper integration_test jbuilder job mailbox mailermigrationmodelresource scaffold scaffold_controller system_test taskActiveRecord: active_record:application_recordTestUnit: test_unit:channel test_unit:generator test_unit:mailbox test_unit:pluginThere are a lot more as you can see and you can easily learn about them by entering rails g <name of generator> in the terminal. Your terminal will output a short description of that generator.
您可以看到更多内容,并且可以在终端中输入rails g <name of generator>来轻松了解它们。 您的终端将输出该生成器的简短描述。
A lifesaver for me is that you can easily erase all traces of the generator you created by simply replacing the g in rails g <generator name> to a d like so, rails d <name of generator>. So don’t be afraid!
用于我的救星是,你可以很容易地通过简单地更换克删除您创建的发电机的所有痕迹rails g <generator name>到广告,像这样, rails d <name of generator> 。 所以不要害怕!
Also, note I added —-note-test-framework in the below examples, this does not create test files. If you need test files simply omit this command.
另外,请注意,我在以下示例中添加了—-note-test-framework ,这不会创建测试文件。 如果您需要测试文件,只需忽略此命令。
Lastly, since summer is almost over and who doesn’t like ice cream?! I chose ice cream as my generator example. Eat up!
最后,由于夏天快结束了,谁不喜欢冰淇淋? 我选择冰淇淋作为发电机示例。 吃吧
The migration generator makes it easy to create and edit tables. To create a migration table follow below. You can also add columns to your command. String is the default datatype, so flavor in the example below is the same as size:string.
迁移生成器使创建和编辑表变得容易。 要创建迁移表,请遵循以下步骤。 您也可以在命令中添加列。 String是默认数据类型,因此下面示例中的flavor与size:string相同。
rails g migration create_ice_creams flavor size:string scoops:integerYou will see the below output telling you your table was created!
您将看到以下输出,告诉您您的表已创建!
Running via Spring preloader in process 57527 invoke active_record create db/migrate/20200906130653_create_ice_creams.rbIf you go into your db/migrate folder you will see the table set up! Now all you need to do is rails db:migrate. Sweet!
如果进入db / migrate文件夹,您将看到表格已设置! 现在您需要做的就是rails db:migrate 。 甜!
To create a model generator type the following in your terminal. Just like the migration generator, you can include attributes.
要创建模型生成器,请在终端中输入以下内容。 就像迁移生成器一样,您可以包含属性。
rails g model IceCream flavor size scoops:integer --no-test-frameworkThe below output tells us our migration table was created with the attributes along with our model file!
下面的输出告诉我们,迁移表是使用属性以及模型文件创建的!
Running via Spring preloader in process 77288 invoke active_record create db/migrate/20200908121113_create_ice_creams.rb create app/models/ice_cream.rbFor the controller generator instead of adding attributes like the model, you can add controller actions.
对于控制器生成器,您可以添加控制器操作,而不是添加模型之类的属性。
rails g controller ice_creams index show --no-test-frameworkYour output will show that you just created quite a few files and folders!
您的输出将显示您刚刚创建了许多文件和文件夹!
Running via Spring preloader in process 58112 create app/controllers/ice_creams_controller.rb route get 'ice_creams/index'get 'ice_creams/show' invoke erb create app/views/ice_creams create app/views/ice_creams/index.html.erb create app/views/ice_creams/show.html.erb invoke helper create app/helpers/ice_creams_helper.rb invoke assets invoke scss create app/assets/stylesheets/ice_creams.scssLet’s see what we created!
让我们看看我们创造了什么!
Our ice_creams_controller.rb with our index and show actions defined 我们的ice_creams_controller.rb及其索引并显示已定义的操作 Routes for index and show 索引和显示的路线 A folder in the views folder called ice_creams with index and show html.erb files views文件夹中的一个名为ice_creams的文件夹,带有索引并显示html.erb文件 helper files 助手文件 stylesheets for the controller 控制器的样式表This is the most powerful out of the four! You can also include attributes for your model.
这是四个中最强大的! 您还可以包括模型的属性。
rails g resource IceCream flavor size scoops:integer --no-test-frameworkYour terminal output will look something like below
您的终端输出如下所示
Running via Spring preloader in process 61311 invoke active_record create db/migrate/20200906152644_create_ice_creams.rb create app/models/ice_cream.rb invoke controller create app/controllers/ice_creams_controller.rb invoke erb create app/views/ice_creams invoke helper create app/helpers/ice_creams_helper.rb invoke assets invoke scss create app/assets/stylesheets/ice_creams.scss invoke resource_route route resources :ice_creamsHere we created…
在这里,我们创建了……
Migration table along with columns that we put as our attributes 迁移表以及我们作为属性放置的列 The model 该模型 Controller with no actions 控制器无动作 Ice_creams folder in views (note: no action html.erb files) 视图中的Ice_creams文件夹(注意:无操作html.erb文件) Helper files 助手文件 Stylesheets for the controller 控制器的样式表 Resource Route for all CRUD actions 所有CRUD动作的资源路线As you can see rails generators are very powerful and can save a lot of time if you know how to utilize them. Hope this helped and happy coding!
如您所见,rails生成器非常强大,如果您知道如何使用它们,则可以节省很多时间。 希望这对您有所帮助并且编码愉快!
翻译自: https://medium.com/@samantha.lurio/rails-generators-cheat-sheet-61e2e58b7be7
rails 单模型多态
相关资源:微信小程序源码-合集6.rar