如何从固定装置填充导轨中的表格?
快速摘要: 我有一个 Rails 应用程序,它是个人清单/待办事项列表。 基本上,您可以登录并管理您的待办事项列表。
我的问题: 当用户创建新帐户时,我想用 20-30 个默认待办事项填充他们的清单。 我知道我可以说:
wash_the_car = ChecklistItem.new
wash_the_car.name = 'Wash and wax the Ford F650.'
wash_the_car.user = @new_user
wash_the_car.save!
...repeat 20 times...
但是,我有 20 个 ChecklistItem 行要填充,因此这将是 60 行非常潮湿(又称为不干燥)的代码。 一定有更好的办法。
因此,我想在创建帐户时使用 YAML 文件中的 ChecklistItems 表作为种子。 YAML 文件可以填充我的所有 ChecklistItem 对象。 当创建一个新用户时——砰! -- 预设的待办事项在其列表中。
我该怎么做呢?
谢谢!
(PS:对于那些想知道我为什么这样做的人:我正在为我的网页设计公司进行客户端登录。我要经历一组 20 个步骤(第一次会议、设计、验证、测试等)这 20 个步骤是我想要为每个新客户填充的 20 个清单项目。但是,虽然每个人都从相同的 20 个项目开始,但我通常会根据项目自定义要采取的步骤。我的普通待办事项列表实现并希望以编程方式填充行)如果您有疑问,我可以进一步解释。
Quick summary:
I have a Rails app that is a personal checklist / to-do list. Basically, you can log in and manage your to-do list.
My Question:
When a user creates a new account, I want to populate their checklist with 20-30 default to-do items. I know I could say:
wash_the_car = ChecklistItem.new
wash_the_car.name = 'Wash and wax the Ford F650.'
wash_the_car.user = @new_user
wash_the_car.save!
...repeat 20 times...
However, I have 20 ChecklistItem rows to populate, so that would be 60 lines of very damp (aka not DRY) code. There's gotta be a better way.
So I want to use seed the ChecklistItems table from a YAML file when the account is created. The YAML file can have all of my ChecklistItem objects to be populated. When a new user is created -- bam! -- the preset to-do items are in their list.
How do I do this?
Thanks!
(PS: For those of you wondering WHY I am doing this: I am making a client login for my web design company. I have a set of 20 steps (first meeting, design, validate, test, etc.) that I go through with each web client. These 20 steps are the 20 checklist items that I want to populate for each new client. However, while everyone starts with the same 20 items, I normally customize the steps I'll take based on the project (and hence my vanilla to-do list implementation and desire to populate the rows programatically). If you have questions, I can explain further.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需写一个函数:
Just write a function:
我同意其他回答者的建议,建议您只需用代码来完成即可。 但它不必像建议的那样冗长。 如果您希望它是这样的话,它已经是一个衬垫:
将其放入从文件中读取的项目循环中,或存储在类中或其他任何地方:
或者如果您的项目复杂性增加,请将字符串数组替换为哈希数组...
但我并不是真的建议您将所有默认值放入
ChecklistItem
内的常量中。 我只是这样描述,以便您可以看到 Ruby 对象的结构。 相反,将它们放入您读取一次并缓存的 YAML 文件中:或者,如果您希望管理员能够动态管理默认选项,请将它们放入数据库中:
很多选项。
I agree with the other answerers suggesting you just do it in code. But it doesn't have to be as verbose as suggested. It's already a one liner if you want it to be:
Throw that in a loop of items that you read from a file, or store in your class, or wherever:
or if your items increase in complexity, replace the array of strings with an array of hashes...
But I'm not really suggesting you throw all the defaults in a constant inside
ChecklistItem
. I just described it that way so that you could see the structure of the Ruby object. Instead, throw them in a YAML file that you read in once and cache:Or if you wand administrators to be able to manage the default options on the fly, put them in the database:
Lots of options.
Rails Fixture 用于填充单元测试的测试数据; 不要认为它应该用于您提到的场景。
我想说的是,只需提取一个新方法
add_checklist_item
即可完成。如果您想要更大的灵活性
该文件可以是一个简单的文本文件,其中每一行对应一个任务描述,例如“清洗福特 F650 并打蜡。”。 用 Ruby 编写应该很容易,
A Rails Fixture is used to populate test-data for unit tests ; Dont think it's meant to be used in the scenario you mentioned.
I'd say just Extract a new method
add_checklist_item
and be done with it.If you want more flexibility
The file can be a simple text file where each line corresponds to a task description like "Wash and wax the Ford F650.". Should be pretty easy to write in Ruby,