Skip to content
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

Usage:

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:

NOTE:

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 />
<% }) %>
Clone this wiki locally