forked from mhayashi/express-helpers
-
Notifications
You must be signed in to change notification settings - Fork 13
form_for
tanema edited this page Mar 18, 2012
·
4 revisions
This a form helper that tries to be similar to that of the Ruby on Rails form helper
For options please see the form_tag
<% form_for("user", {multipart: true}, function(f){ %>
<%- f.label_for("username") %>
<%- f.text_field("username") %><br />
<%- f.submit({class: 'button'}) %><br />
<% }) %>
=>
<form action='/users' method='post' enctype='multipart/form-data' ><label for='user[username]' >Username</label>
<input id='user[username]' value='' type='text' name='user[username]' /><br />
<input class='button' type='submit' value='Submit' /></form><br />
Or without html options
<% form_for("user", function(f){ %>
<%- f.label_for("username") %>
<%- f.text_field("username") %><br />
<%- f.submit({class: 'button'}) %><br />
<% }) %>
=>
<form action='/users' method='post' enctype='application/x-www-form-urlencoded' ><label for='user[username]' >Username</label>
<input id='user[username]' value='' type='text' name='user[username]' /><br />
<input class='button' type='submit' value='Submit' /></form><br />
the form_for method takes the same inputs as the form_tag but the action (here shown as "user") is also the object that is being worked with ie.
res.render('test', {user: {username: 'tim', name: 'Tim'}});
this in the future will automatically fill in values for you if they exist in the object. Also this will fill in the action in this case as "/user"
helpers that can be used in a form are:
- checkbox
- color_field
- date
- date_time
- email_field
- file_field
- hidden_field
- image_submit
- label_for
- number_field
- password_field
- phone_field / telephone_field_tag
- radio
- reset
- search_field
- select
- strip_links
- submit
- text_area
- text_field
- time
- url_field
Since the form tags are appended before the first form helper tag and after the last form helper tag, there may be a case where you want something inside the form but before or after these so you can use the shim method for this.
<% form_for("user", function(f){ %>
<%- f.shim() %>
this is inside the form: <%- user.username %>
<%- f.label_for("username") %>
<%- f.text_field("username") %><br />
<%- f.submit({class: 'button'}) %><br />
<% }) %>