rails5 创建项目_rails项目以及如何创建怪物

rails5 创建项目

So here we are at the Rails Project stage, it feels like only yesterday I was doing simple addition programming and being wowed by it. Now I am making 11 model monsters with multiple has_many to has_many relationships.

因此,这里我们处于Rails Project阶段,感觉就像昨天一样,我只是在做简单的加法编程,并为此感到惊讶。 现在,我正在制作11个具有多个has_many到has_many关系的模型怪兽。

What a lot can change in just a few months! I decided to take a different tack for this project and move away from Dungeons and Dragons, Instead I am now in the realm of designing a miniature game (a hybrid from two different rulesets I love). This is how we ask the question, how do you create a monster?

短短几个月内可以改变很多! 我决定对该项目采取不同的策略,并远离《龙与地下城》,而现在我正在设计一个微型游戏(我喜欢的两个不同规则集的混合物)。 这就是我们提出的问题,即如何创建怪物?

The answer is, little step by little step. It may seem like obvious advice but one thing I am really learning here is limiting the scope of what you are working on to one step at a time. First off create your basic Users access, sessions and single model relationship (in my case for the above app, its Users have warbands, warbands belong to a user)

答案是,一步一步走。 这似乎是显而易见的建议,但是我在这里真正要学习的一件事是将您正在处理的内容的范围限制为一次。 首先,创建基本的用户访问权限,会话和单一模型关系(在我的情况下,对于上述应用,其用户有战队,战队属于某个用户)

From there add one layer at a time, I consecutively have now added warriors (which belong to warbands), then skills, equipment, armour etc. So the warriors can also be equipped and good at what they do!

从那里一次增加一层,现在我已经连续增加了战士(属于战队),然后增加了技能,装备,盔甲等。因此,战士也可以装备并擅长于他们的工作!

Using validations and helper methods was important in allowing only those who are actually A. logged in, and B. actually the owners; edit and modify any of their own warbands and warriors.

使用验证和帮助方法对于仅允许实际上是A.登录的用户和B.实际上是所有者的用户而言非常重要。 编辑和修改自己的任何战队和战士。

Image for post
A couple of my very useful helper methods
我的几个非常有用的辅助方法

Use of the before_action: in the requisite controllers also helps keep the code lovely and clear of repeat code. This just means that you are free to make everything a lot more understandable to anyone else who is wanting to work on the project!

在必要的控制器中使用before_action:还有助于保持代码美观,并避免重复代码。 这只是意味着您可以自由地使所有想要从事该项目的人都更容易理解所有内容!

Image for post
before actions
行动之前

Using validations is a great way to make sure you are getting the data you need for your app to run smoothly as well, such as in my case, when a User is made the e-mail is not already in use, and the password meets the required complexity. Or Warbands have a name, and that name is at least 4 characters long when a new warband is created. This just saves your database just getting filled with useless dummy data.

使用验证是一种很好的方式,可以确保您获取所需的数据以确保应用程序也能顺利运行,例如,在我的情况下,当使用用户身份时,电子邮件尚未使用且密码符合要求所需的复杂性。 或战队有一个名称,并且在创建新战队时该名称至少长4个字符。 这只会节省您的数据库,使它们充满无用的伪数据。

Validations are run by rails before saving a new object, if any are invalid then it throws an error! Handy ey! It also allows the programmer to call the .valid? method to double check if an object has been made valid by rails or has failed validations, but more about that shortly.

验证是在保存新对象之前由rails运行的,如果有任何无效对象,则会引发错误! 得心应手! 它还允许程序员调用.valid? 仔细检查对象是否已通过Rails使其有效或验证失败的方法,但是稍后将对此进行更多介绍。

To add validations you do so in the Model that you wish to validate and you have to specify the field you wish to validate (in the below case its the name field for Warband)

要添加验证,您可以在要验证的模型中进行验证,并且必须指定要验证的字段(在以下情况下,其为Warband的名称字段)

Image for post
Validation at the top
顶部验证

There are a great number of ways to validate some very handy and quite simple to use ones are as follows

有很多方法可以验证一些非常方便且非常简单的方法,如下所示

Inclusion: Allows you to set specific parameters that the field has to match, it uses the option in: to allow you to add the required fields. With the %w turning the following brackets into a word array. Example provided below.

包含:允许您设置字段必须匹配的特定参数,它使用以下选项:允许您添加必填字段。 使用%w,将以下方括号变成一个单词数组。 下面提供了示例。

validates :type_of_mustelid, inclusion: { in: %w(ferret weasel stoat),

validates :type_of_mustelid , inclusion: { in: %w(ferret weasel stoat),

Length: Allows you to set a minimum/maximum/both length for the field.

长度:允许您设置字段的最小/最大/两个长度。

validates :name_of_pet, length: { minimum: 2 }

validates :name_of_pet , length: { minimum: 2 }

validates :pet_description, length: { maximum: 500 }

validates :pet_description , length: { maximum: 500 }

validates :pets_age, length: { in: 1..5 }

validates :pets_age , length: { in: 1..5 }

Numericality: Will check if a field is given as a number, and it has a number of very useful options within it. Standardly it will convert anything entered into a float if it is a number, however you have the option :only_integer. To disallow any floats e.g it you were after a pets age and you didnt want to worry about a dog thats 4 & 3/4 years old!

数字:将检查字段是否为数字,并且其中包含许多非常有用的选项。 通常,它将是数字的任何内容都转换为浮点数,但是您可以选择:only_integer。 为了禁止任何漂浮物,例如,您是在宠物年龄之后的,并且您不想担心那只4岁和3/4岁的狗!

validates :number_of_hamster, numericality: { only_integer: true }

validates :number_of_hamster , numericality: { only_integer: true }

A load of other options are provided such as

提供了许多其他选项,例如

:even, must be even

:even,必须是偶数

:odd, must be odd

:odd,必须为奇数

:greater_than, must be greater than

:greater_than,必须大于

:greater_than_or_equal_to, must be greater than or equal to

:greater_than_or_equal_to,必须大于或等于

:less_than, must be less_than

:less_than,必须小于

:less_than_or_equal_to, must be less than or equal to

:less_than_or_equal_to,必须小于或等于

:equal_to, must be equal

:equal_to,必须相等

:other_than, must be anything but.

:other_than,一定不能是。

Presence: Checks that the field has been filled in with something other than nil or a blank string using the .empty? method. Usually important for making sure the fields that are really important are filled out

存在:使用.empty检查该字段是否已填充nil或空白字符串以外的内容。 方法。 通常对于确保填写真正重要的字段很重要

validates :pets_favourite_music, presence: true

validates :pets_favourite_music , presence: true

Absence: Does the inverse of presence and makes sure that the field is left blank (either whitespace or nil) using the .present? method

缺席:是否使用.present来倒数存在并确保将字段保留为空白(空白或nil)? 方法

validates :names_of_bad_dogs, absence: true

p.s they’re good dogs brent.

ps他们是布伦特的好狗。

https://twitter.com/dog_rates/status/775410014383026176?lang=en

https://twitter.com/dog_rates/status/775410014383026176?lang=zh-CN

Uniqueness: checks to make sure that the entered field matches no other already saved version of that model (We can only have artist known as prince)

唯一性:检查以确保输入的字段与该模型的其他任何已保存版本都不匹配(我们只能将艺术家称为王子)

validates :artist, uniqueness: true

You can also use scope methods with the uniqueness trait to make sure that it does not cover ALL records, instead only those within the scope you are looking at.

您还可以使用具有唯一性特征的范围方法,以确保它不涵盖所有记录,而仅涵盖您正在查看的范围内的那些记录。

A great thing about validations, is how they throw absolutely lovely error messages out for you! These can be accessed via

验证的一大优点是它们如何为您抛出绝对可爱的错误消息! 这些可以通过访问

Model_name.errors.messages 

which you can then allow to pop up on screen for the discerning user to be able to see what field they filled in wrong. Rather more useful than just the form repeatedly failing to submit because the User did not know that had to make sure their pet had a favourite song!

然后您可以允许其在屏幕上弹出,以供有眼光的用户查看他们填写错误的字段。 它不仅比表单一遍又一遍地提交失败更有用,因为用户不知道必须确保自己的宠物有喜欢的歌曲!

Image for post
Checking validation using the .valid? method
使用.valid检查验证? 方法
Image for post
warband_alerts method setting any errors so they will be posted on the screen!
warband_alerts方法设置任何错误,以便将其发布在屏幕上!

Anyways, I hope this made some sense to y’all and you have found it handy! Validations are a real handy tool to get you head around, but once you have they are quite simple really!

无论如何,我希望这对大家都有意义,并且您发现它很方便! 验证是一个真正方便的工具,它可以助您一臂之力,但是一旦获得验证,它们就非常简单!

Feel free to suggest anything else that you would like me to go into a deep dive of, and I shall endeavour to make a blog post about it!

随意提出其他任何您想让我深入探讨的建议,我将尽力就此发表一篇博客文章!

Dan

翻译自: https://medium.com/swlh/rails-project-and-how-to-create-a-monster-989327e1a7cb

rails5 创建项目

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值