forked from robin/chitsheet
/
select_box.yml
59 lines (45 loc) · 2.03 KB
/
select_box.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
---
select_box: |-
---
This select box serves two purposes in a view, say a 'new' or 'edit' view:
1. The user can choose a pre-existing object by selecting from a dropdown
list of the existing objects, rather than typing into an entry field.
2. The user can reference the object by a friendly name rather than by id.
In the 'foobar' Person view, to select a reference to a Person:
<% form_for([foobar]) do |f| %>
<%= f.label "Name:" %>
<%= f.select :person_id, Person.select_list %>
And in the Person model:
def self.select_list
self.find(:all).map { |p| [p.name, p.id] }
end
To choose from a list of a subset of all People:
In the view:
<%= f.label "Member:" %>
<%= f.select :person_id, Person.select_member_list %>
And in the Person model:
def self.select_member_list
self.find(:all).map { |p| [p.name, p.id] if p.member? }.compact
end
--
When there is a one-to-many association involved, and you are presenting
a choice from among the existing association objects, there is an easy
collection_select method that the belongs_to association gives you. As
Ryan Bates discusses in Railscasts episode 057, in the view to create a
Product, to choose its Category, you can use:
<%= f.collection_select :category_id, Category.all, :id, :name,
:prompt => "Select a category:" %>
To provide the option to define & choose a new Category when creating a
Product, you can add the Category creation to the Person model and view.
The belongs_to association provides the 'create_[associated model]'
method.
In the view:
... Or create a new category:
<%= f.text_field :new_category_name %>
And in the Person model, define the new attribute (new_category_name),
and a before_save callback to create the new category if entered:
attr_accessor :new_category_name
before_save :create_category_from_name
def create_category_from_name
create_category(:name => new_category_name) unless \
new_category_name.blank?