Update rails-forms
This commit is contained in:
parent
32cc5bec63
commit
d856403c44
130
rails-forms.md
130
rails-forms.md
|
@ -3,58 +3,92 @@ title: Rails form helpers
|
|||
layout: default
|
||||
---
|
||||
|
||||
### Forms
|
||||
## Form builder (form_for)
|
||||
|
||||
# Model:
|
||||
form_for @post do |f|
|
||||
### Building forms
|
||||
|
||||
form_for @post,
|
||||
url: { method: 'put', action: 'create' },
|
||||
```haml
|
||||
- form_for @post do |f|
|
||||
```
|
||||
|
||||
### Options
|
||||
|
||||
```haml
|
||||
- form_for @post, |
|
||||
url: { method: 'put', action: 'create' }, |
|
||||
html: { class: 'nifty_form' } do |f|
|
||||
```
|
||||
|
||||
f.label :first_name
|
||||
f.text_field :first_name
|
||||
### Text
|
||||
|
||||
field :multiselect, f, :first_name
|
||||
|
||||
### Stuff
|
||||
|
||||
f.object
|
||||
|
||||
### Fields for
|
||||
|
||||
= form_for @post do |f|
|
||||
= fields_for :author, @post.author do |ff|
|
||||
= ff.text_field :name
|
||||
|
||||
### Fields
|
||||
|
||||
f.check_box :is_admin
|
||||
```rb
|
||||
f.text_field :title
|
||||
f.text_area :body,
|
||||
size: '60x12'
|
||||
f.text_area :body, size: '60x12'
|
||||
```
|
||||
|
||||
### 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"
|
||||
f.radio_button :gender, 'female'
|
||||
f.label :gender_female, "Female"
|
||||
```
|
||||
|
||||
|
||||
### Label
|
||||
|
||||
```rb
|
||||
f.label :post, :title
|
||||
f.label :post, :title, "Title"
|
||||
f.label :post, :title, "Title", class: "title"
|
||||
f.label(:post, :terms) { "Accept terms" }
|
||||
#=> <label for="post_title">Title</labele>
|
||||
```
|
||||
|
||||
radio_button("post", "category", "rails")
|
||||
radio_button("post", "category", "java")
|
||||
#=> <input type="radio" id="post_category_rails" name="post[category]"
|
||||
# value="rails" checked="checked" />
|
||||
### The model
|
||||
|
||||
```ruby
|
||||
f.object
|
||||
```
|
||||
|
||||
### Fields for
|
||||
|
||||
```haml
|
||||
= form_for @post do |f|
|
||||
= fields_for :author, @post.author do |ff|
|
||||
= ff.text_field :name
|
||||
```
|
||||
|
||||
### Select dropdowns
|
||||
|
||||
f.time_zone_select :time_zone
|
||||
f.date_select :birthday
|
||||
|
||||
```rb
|
||||
f.select :city_id, [['Lisbon',1], ['Madrid',2], ...], 4 # (4 = selected)
|
||||
|
||||
f.collection_select :city_id, City.all, :id, :name
|
||||
|
||||
options_for_select [['Lisbon',1], ['Madrid', 2], ...], 4 # Just makes <option> tags
|
||||
```
|
||||
|
||||
### Collections
|
||||
|
||||
```
|
||||
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)
|
||||
```
|
||||
|
||||
### Time select
|
||||
|
||||
```rb
|
||||
f.time_zone_select :time_zone
|
||||
f.date_select :birthday
|
||||
```
|
||||
|
||||
### The rest
|
||||
|
||||
|
@ -63,11 +97,31 @@ layout: default
|
|||
|
||||
### I18n
|
||||
|
||||
helpers.submit.create = "Create a %{model}"
|
||||
helpers.submit.update = "Confirm changes to %{model}"
|
||||
```yaml
|
||||
helpers:
|
||||
submit:
|
||||
# helpers.submit.<action>
|
||||
create: "Create a %{model}"
|
||||
update: "Confirm changes to %{model}"
|
||||
|
||||
helpers.submit.article.create = "Publish article"
|
||||
helpers.submit.article.update = "Update article"
|
||||
# helpers.submit.<model>.<action>
|
||||
article:
|
||||
create: "Publish article"
|
||||
update: "Update article"
|
||||
|
||||
helpers.label.post.body = "Write your body text here"
|
||||
# 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" />
|
||||
```
|
||||
|
|
Loading…
Reference in New Issue