简体   繁体   English

收集和存储物理地址或邮寄地址的规范 Rails 方式是什么?

[英]What is the canonical Rails way to collect & store physical or mailing addresses?

I need to collect and store mailing addresses in a Rails (2.3) app.我需要在 Rails (2.3) 应用程序中收集和存储邮寄地址。 Is there a Rails way to do mailing addresses?是否有 Rails 方式来处理邮寄地址? For example, I would do f.date_select to generate a series of dropdowns to handle dates, which are then stored in the database, which I defined in schema and migrations as t.datetime "foo" .例如,我会做f.date_select来生成一系列下拉列表来处理日期,然后将它们存储在数据库中,我在架构和迁移t.datetime "foo"其定义为t.datetime "foo" I would consider that the canonical Rails way to handle dates, and am curious if there is an analogue for physical addresses, postal codes, etc.我会考虑使用规范的 Rails 处理日期的方式,并且很好奇是否有类似的物理地址、邮政编码等。

There isn't a set system or helper for this, because unlike the date picker, which is more complicated than it seems, collecting address info is pretty straightforward.对此没有固定的系统或帮助程序,因为与比看起来更复杂的日期选择器不同,收集地址信息非常简单。 However, if you need to collect address info in your app and attach it to more than one thing, you can use a combination of polymorphic associations, nested attributes, and a shared view.但是,如果您需要在应用程序中收集地址信息并将其附加到多个事物上,则可以使用多态关联、嵌套属性和共享视图的组合。

I give a basic example and full explanation in my blog post here:我在我的博客文章中给出了一个基本的例子和完整的解释:

http://kconrails.com/2010/10/19/common-addresses-using-polymorphism-and-nested-attributes-in-rails/ http://kconrails.com/2010/10/19/common-addresses-using-polymorphism-and-nested-attributes-in-rails/

It might seem daunting at first, but it's really very simple.乍一看可能令人生畏,但实际上非常简单。 The benefit is that your addresses are uniform across all views in your app.好处是您的地址在应用程序中的所有视图中都是统一的。 And every object with an address has the exact same setup, even as you change addresses in the future.并且每个具有地址的对象都具有完全相同的设置,即使您将来更改地址也是如此。

I think this is ultimately what you were looking for.我认为这最终是你要找的。 I hope it helps!我希望它有帮助!

No. Addresses, postal codes, etc... are just considered as strings/integers.不。地址、邮政编码等……仅被视为字符串/整数。 There is no database specific type to map to these items and no rails specific classes for these items either.没有数据库特定类型映射到这些项目,也没有这些项目的特定于 rails 的类。

I am also unfamiliar with any ActiveSupport (a library used b Rails) support for mailing addresses either, but I may be wrong here.我也不熟悉对邮寄地址的任何 ActiveSupport(使用 b Rails 的库)支持,但我在这里可能是错的。

It is possible if you search for Gems that you may find one, that makes operating with these items easier.如果您搜索 Gems,您可能会找到一个,这使得操作这些项目变得更加容易。

I have started looking into this - addresses are not straightforward on an international level.我已经开始研究这个 - 地址在国际层面上并不简单。 So far I have found a couple of gems:到目前为止,我发现了一些宝石:

Even if you end up not using any of these gems, it might be worth looking at how they store different parts of the address.即使您最终没有使用这些 gem 中的任何一个,也可能值得研究它们如何存储地址的不同部分。 The general consensus is to have a polymorphic address table, but there are some variations on what columns you need to have.普遍的共识是拥有一个多态地址表,但是您需要拥有哪些列有一些变化。

This article provides some examples of different ways you can handle the form to be as specific or generic as you need across countries, mentions some of the inconsistencies you might encounter in different regions (even within a given country): http://www.uxmatters.com/mt/archives/2008/06/international-address-fields-in-web-forms.php本文提供了一些示例,说明您可以根据需要在不同国家/地区处理特定或通用的表格,并提到您在不同地区(甚至在给定国家/地区)可能会遇到的一些不一致问题: http://www. uxmatters.com/mt/archives/2008/06/international-address-fields-in-web-forms.php

For instance, you don't want to make zip/postal code a required field, since not all countries have one.例如,您不想将邮政编码设为必填字段,因为并非所有国家/地区都有。 Depending on how complex you want to go, you may want to ask the country first, and have different forms depending on the answer to that question.根据您想去的复杂程度,您可能想先询问国家/地区,然后根据该问题的答案采用不同的形式。 There is a good blog post on avoiding ambiguity, particularly in respect to address line1 and address line2 fields: https://baymard.com/blog/address-line-2有一篇关于避免歧义的好博文,特别是关于地址 line1 和 address line2 字段: https : //baymard.com/blog/address-line-2

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

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