form(record_name, options = {}) public

Returns an entire form with all needed input tags for a specified Active Record object. For example, if @post has attributes named title of type VARCHAR and body of type TEXT then


would yield a form like the following (modulus formatting):

  <form action='/posts/create' method='post'>
      <label for="post_title">Title</label><br />
      <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />
      <label for="post_body">Body</label><br />
      <textarea cols="40" id="post_body" name="post[body]" rows="20"></textarea>
    <input name="commit" type="submit" value="Create" />

It’s possible to specialize the form builder by using a different action name and by supplying another block renderer. For example, if @entry has an attribute message of type VARCHAR then

    :action => "sign",
    :input_block => Proc.new { |record, column|
      "#{column.human_name}: #{input(record, column.name)}<br />"

would yield a form like the following (modulus formatting):

  <form action="/entries/sign" method="post">
    <input id="entry_message" name="entry[message]" size="30" type="text" /><br />
    <input name="commit" type="submit" value="Sign" />

It’s also possible to add additional content to the form by giving it a block, such as:

  form("entry", :action => "sign") do |form|
    form << content_tag("b", "Department")
    form << collection_select("department", "id", @departments, "id", "name")

The following options are available:

  • :action - The action used when submitting the form (default: create if a new record, otherwise update).
  • :input_block - Specialize the output using a different block, see above.
  • :method - The method used when submitting the form (default: post).
  • :multipart - Whether to change the enctype of the form to "multipart/form-data", used when uploading a file (default: false).
  • :submit_value - The text of the submit button (default: "Create" if a new record, otherwise "Update").
Show source
Register or log in to add new notes.