cheatsheets/rails-forms.md

152 lines
2.3 KiB
Markdown
Raw Permalink Normal View History

2013-10-14 02:36:58 +00:00
---
title: Rails form helpers
2015-11-24 05:02:17 +00:00
category: Rails
2012-11-15 21:57:56 +00:00
---
2015-04-16 08:02:28 +00:00
## Form builder
2012-11-15 21:57:56 +00:00
### Form builder
2015-04-16 07:55:46 +00:00
```haml
- form_for @post do |f|
```
2012-11-15 21:57:56 +00:00
2015-04-16 08:02:28 +00:00
Field names will be prefixed with `post` (the class name), and values will be derived from this object (eg, `f.text_field :name` from `@post.name`).
2015-04-16 07:55:46 +00:00
### Options
2014-02-25 10:32:14 +00:00
2015-04-16 07:55:46 +00:00
```haml
- form_for @post, |
url: { method: 'put', action: 'create' }, |
2015-04-16 08:02:28 +00:00
html: { class: 'nifty_form' } |
do |f|
2015-04-16 07:55:46 +00:00
```
2014-02-25 10:32:14 +00:00
2015-04-16 08:02:28 +00:00
## Fields
2015-04-16 07:55:46 +00:00
### Text
2014-02-25 10:32:14 +00:00
2015-04-16 07:55:46 +00:00
```rb
f.text_field :title
f.text_area :body, size: '60x12'
```
2012-11-15 21:57:56 +00:00
2015-04-16 07:55:46 +00:00
### Checkbox
```rb
f.check_box :remember_me
f.label :remember_me, "Remember me"
```
### Radio
```rb
f.radio_button :gender, 'male'
f.label :gender_male, "Male"
2015-04-16 08:02:28 +00:00
2015-04-16 07:55:46 +00:00
f.radio_button :gender, 'female'
f.label :gender_female, "Female"
```
### Label
2014-10-04 15:43:09 +00:00
2015-04-16 07:55:46 +00:00
```rb
2015-04-16 08:02:28 +00:00
f.label :title
f.label :title, "Title"
f.label :title, "Title", class: "title"
2015-04-16 07:55:46 +00:00
f.label(:post, :terms) { "Accept terms" }
```
2014-10-04 15:43:09 +00:00
2015-04-16 08:02:28 +00:00
### Submit button
```rb
f.submit "Create"
```
### Hidden fields
```rb
f.hidden_field :id
```
## Misc
2015-04-16 07:55:46 +00:00
### The model
2012-11-15 21:57:56 +00:00
2015-04-16 07:55:46 +00:00
```ruby
f.object
```
2014-02-25 10:32:14 +00:00
2015-04-16 07:55:46 +00:00
### Fields for
2014-02-25 10:32:14 +00:00
2015-04-16 07:55:46 +00:00
```haml
= form_for @post do |f|
= fields_for :author, @post.author do |ff|
= ff.text_field :name
```
2012-11-15 21:57:56 +00:00
### Select dropdowns
2015-04-16 07:55:46 +00:00
```rb
2015-04-16 08:02:28 +00:00
f.select :city_id, [['Lisbon',1], ['Madrid',2], ...], 4
# (4 = selected)
2015-04-16 07:55:46 +00:00
2015-04-16 08:02:28 +00:00
options_for_select [['Lisbon',1], ['Madrid',2], ...], 4
# Just makes <option> tags
2015-04-16 07:55:46 +00:00
```
### Collections
2012-11-15 21:57:56 +00:00
2015-04-16 07:55:46 +00:00
```
f.collection_radio_buttons :author_id, Author.all, :id, :name_with_initial
f.collection_select :city_id, City.all, :id, :name
# (field, collection, value_key, label_key)
```
2012-11-15 21:57:56 +00:00
2015-04-16 07:55:46 +00:00
### Time select
2012-11-15 21:57:56 +00:00
2015-04-16 07:55:46 +00:00
```rb
f.time_zone_select :time_zone
f.date_select :birthday
```
2014-02-25 10:32:14 +00:00
### I18n
2015-04-16 07:55:46 +00:00
```yaml
helpers:
submit:
# helpers.submit.<action>
create: "Create a %{model}"
update: "Confirm changes to %{model}"
# helpers.submit.<model>.<action>
article:
create: "Publish article"
update: "Update article"
# helpers.label.<model>.<field>
label:
post:
body: "Your body text"
```
### Outside `f`
```rb
radio_button("post", "category", "rails")
radio_button("post", "category", "java")
# picks from @post.category
# <input type="radio" id="post_category_rails" name="post[category]"
# value="rails" checked="checked" />
```
2015-11-13 00:58:37 +00:00
### Reference
```rb
select(method, choices = nil, options = {}, html_options = {}, &block)
choices == [ ['label', id], ... ]
submit(value=nil, options={})
```