Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. Is there a Rails way to do mailing addresses? 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". 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.

like image 727
Alan H. Avatar asked Oct 20 '10 00:10

Alan H.


1 Answers

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/

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!

like image 82
Jaime Bellmyer Avatar answered Oct 05 '22 23:10

Jaime Bellmyer