# Select in form - best practice?

**URL:** <https://www.ruby-forum.com/t/select-in-form-best-practice/152630>\
**Category:** Rails\
**Created:** [November 5, 2008, 8:31pm UTC](https://www.ruby-forum.com/t/select-in-form-best-practice/152630 "2008-11-05T20:31:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [November 5, 2008, 8:31pm UTC](https://www.ruby-forum.com/t/select-in-form-best-practice/152630/1 "2008-11-05T20:31:59Z")

</div>

Q. What resource do readers recommend for referencing regarding best  
practices for form construction in Rails?

I have the following situation: location\_type of Locations may have one  
of five (5) possible codes. Presently I have these codes listed in a  
drop-down select constructed thusly:

**Location type**  
 \<%= f.select :location\_type, [['MAIN - Main Address', 'MAIN'], ['POST - Postal Delivery', 'POST'], ['DELV - Package Delivery', 'DELV'], ['SHIP - Package Shipping', 'SHIP'], ['OTHR - Other', 'OTHR'], ], :selected =\> 'MAIN', :size =\> 4 -%\>

Should this instead be constructed so that the model, Locations, has  
these virtual attributes:

```
valid_location_type[]=
  [
    ['MAIN - Main Location','MAIN'],
    ['POST - Postal Delivery', 'POST'],
  ...
  ]

default_location_type = 'MAIN'

```

and then the view could have this instead:

```
  <%= f.select :location_type,
                @location.valid_location_type,
                :selected => @location.default_location_type,
                :size => 4

```

Would this even work?

Comments?

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [November 5, 2008, 8:46pm UTC](https://www.ruby-forum.com/t/select-in-form-best-practice/152630/2 "2008-11-05T20:46:03Z")

</div>

On Nov 5, 7:31 pm, James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> default_location_type = 'MAIN'
> 
> ```

Personally I would have it as a constant of the Location class, ie

class Location …  
VALID\_TYPES = […]  
end

Fred

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [November 5, 2008, 9:02pm UTC](https://www.ruby-forum.com/t/select-in-form-best-practice/152630/3 "2008-11-05T21:02:55Z")

</div>

Frederick C. wrote:

> On Nov 5, 7:31ï¿½pm, James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > ï¿½ ï¿½ default\_location\_type = ‘MAIN’
> 
> Personally I would have it as a constant of the Location class, ie
> 
> class Location …  
> VALID\_TYPES = […]  
> end
> 
> Fred

Point taken.

Nonetheless, in the interim I have tested it and this does indeed work  
as expected. However, I used private methods and public getters to  
implement.

def valid\_location\_type  
virt\_valid\_type  
end

private

def virt\_valid\_type  
vvt =  
[  
[‘MAIN - Main Address’, ‘MAIN’],  
[‘POST - Postal Delivery’, ‘POST’],  
[‘DELV - Package Delivery’, ‘DELV’],  
[‘SHIP - Package Shipping’, ‘SHIP’],  
[‘OTHR - Other’, ‘OTHR’]  
]  
end

But my question remains: Is this the preferred idiom or is there  
another recommended way to do this?

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [November 5, 2008, 9:44pm UTC](https://www.ruby-forum.com/t/select-in-form-best-practice/152630/4 "2008-11-05T21:44:58Z")

</div>

Looks like a good place to give this a spin:

> **[\#121 Non Active Record Model - RailsCasts](http://railscasts.com/episodes/121-non-active-record-model)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

Basically, use a pure-ruby class to wrap the location types and let it  
support an ActiveRecord like interface (mainly find and all). Then  
you can do collection\_select the way you would with a normal ARec  
model.

Depending on which version of Rails you’re working with you may also  
check into PassiveRecord. It’s a pretty advanced version of the same  
concept. Unfortunately it has dependencies on older versions of ARec.

On Nov 5, 3:02 pm, James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [November 6, 2008, 6:14pm UTC](https://www.ruby-forum.com/t/select-in-form-best-practice/152630/5 "2008-11-06T18:14:11Z")

</div>

I think Fred’s given the preferred idiom. It’s simple & it works.
