method
form
v1.2.6 -
Show latest stable
-
0 notes -
Class: ActionView::Helpers::ActiveRecordHelper
- 1.0.0 (0)
- 1.1.6 (0)
- 1.2.6 (2)
- 2.0.3 (0)
- 2.1.0 (10)
- 2.2.1 (0)
- 2.3.8 (0)
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2
- 4.1.8
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7
- 5.2.3
- 6.0.0
- 6.1.3.1
- 6.1.7.7
- 7.0.0
- 7.1.3.2
- 7.1.3.4
- What's this?
form(record_name, options = {})
public
Returns an entire form with all needed input tags for a specified Active Record object. For example, let’s say you have a table model Post with attributes named title of type VARCHAR and body of type TEXT:
form("post")
That line would yield a form like the following:
<form action='/post/create' method='post'> <p> <label for="post_title">Title</label><br /> <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /> </p> <p> <label for="post_body">Body</label><br /> <textarea cols="40" id="post_body" name="post[body]" rows="20"> </textarea> </p> <input type='submit' value='Create' /> </form>
It’s possible to specialize the form builder by using a different action name and by supplying another block renderer. For example, let’s say you have a model Entry with an attribute message of type VARCHAR:
form("entry", :action => "sign", :input_block => Proc.new { |record, column| "#{column.human_name}: #{input(record, column.name)}<br />" }) => <form action='/post/sign' method='post'> Message: <input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /><br /> <input type='submit' value='Sign' /> </form>
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") end