aboutsummaryrefslogtreecommitdiffstats
path: root/guides
diff options
context:
space:
mode:
authorRashmi Yadav <rays.rashmi@gmail.com>2014-03-08 20:23:15 +0100
committerRashmi Yadav <rays.rashmi@gmail.com>2014-03-08 20:23:15 +0100
commit7ea0a50126a5b8f988fcf1c3fb0c57bdb52918c7 (patch)
tree0f5f0561e1881b804a60daf2cebc0baa2ba58a7f /guides
parent70ff31d69f017d1bc674b913865d5a008de0c8a6 (diff)
downloadrails-7ea0a50126a5b8f988fcf1c3fb0c57bdb52918c7.tar.gz
rails-7ea0a50126a5b8f988fcf1c3fb0c57bdb52918c7.tar.bz2
rails-7ea0a50126a5b8f988fcf1c3fb0c57bdb52918c7.zip
Updating select helper doc [ci skip]
Diffstat (limited to 'guides')
-rw-r--r--guides/source/form_helpers.md10
1 files changed, 10 insertions, 0 deletions
diff --git a/guides/source/form_helpers.md b/guides/source/form_helpers.md
index 455dc7bebe..205e0f6b62 100644
--- a/guides/source/form_helpers.md
+++ b/guides/source/form_helpers.md
@@ -474,6 +474,16 @@ As with other helpers, if you were to use the `select` helper on a form builder
<%= f.select(:city_id, ...) %>
```
+You can also pass a block to `select` helper:
+
+```erb
+<%= f.select(:city_id) do %>
+ <% [['Lisbon', 1], ['Madrid', 2]].each do |c| -%>
+ <%= content_tag(:option, c.first, value: c.last) %>
+ <% end %>
+<% end %>
+```
+
WARNING: If you are using `select` (or similar helpers such as `collection_select`, `select_tag`) to set a `belongs_to` association you must pass the name of the foreign key (in the example above `city_id`), not the name of association itself. If you specify `city` instead of `city_id` Active Record will raise an error along the lines of ` ActiveRecord::AssociationTypeMismatch: City(#17815740) expected, got String(#1138750) ` when you pass the `params` hash to `Person.new` or `update`. Another way of looking at this is that form helpers only edit attributes. You should also be aware of the potential security ramifications of allowing users to edit foreign keys directly.
### Option Tags from a Collection of Arbitrary Objects