More updates to rails forms

This commit is contained in:
Rico Sta. Cruz 2015-04-16 16:02:28 +08:00
parent d856403c44
commit 9e06910aa0
1 changed files with 29 additions and 17 deletions

View File

@ -3,22 +3,25 @@ title: Rails form helpers
layout: default layout: default
--- ---
## Form builder (form_for) ## Form builder
### Building forms
```haml ```haml
- form_for @post do |f| - form_for @post do |f|
``` ```
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`).
### Options ### Options
```haml ```haml
- form_for @post, | - form_for @post, |
url: { method: 'put', action: 'create' }, | url: { method: 'put', action: 'create' }, |
html: { class: 'nifty_form' } do |f| html: { class: 'nifty_form' } |
do |f|
``` ```
## Fields
### Text ### Text
```rb ```rb
@ -38,21 +41,34 @@ f.label :remember_me, "Remember me"
```rb ```rb
f.radio_button :gender, 'male' f.radio_button :gender, 'male'
f.label :gender_male, "Male" f.label :gender_male, "Male"
f.radio_button :gender, 'female' f.radio_button :gender, 'female'
f.label :gender_female, "Female" f.label :gender_female, "Female"
``` ```
### Label ### Label
```rb ```rb
f.label :post, :title f.label :title
f.label :post, :title, "Title" f.label :title, "Title"
f.label :post, :title, "Title", class: "title" f.label :title, "Title", class: "title"
f.label(:post, :terms) { "Accept terms" } f.label(:post, :terms) { "Accept terms" }
#=> <label for="post_title">Title</labele>
``` ```
### Submit button
```rb
f.submit "Create"
```
### Hidden fields
```rb
f.hidden_field :id
```
## Misc
### The model ### The model
```ruby ```ruby
@ -70,9 +86,11 @@ f.object
### Select dropdowns ### Select dropdowns
```rb ```rb
f.select :city_id, [['Lisbon',1], ['Madrid',2], ...], 4 # (4 = selected) f.select :city_id, [['Lisbon',1], ['Madrid',2], ...], 4
# (4 = selected)
options_for_select [['Lisbon',1], ['Madrid', 2], ...], 4 # Just makes <option> tags options_for_select [['Lisbon',1], ['Madrid',2], ...], 4
# Just makes <option> tags
``` ```
### Collections ### Collections
@ -89,12 +107,6 @@ f.collection_select :city_id, City.all, :id, :name
f.time_zone_select :time_zone f.time_zone_select :time_zone
f.date_select :birthday f.date_select :birthday
``` ```
### The rest
f.submit "Create"
f.hidden_field
### I18n ### I18n
```yaml ```yaml