--- !ruby/object:RI::MethodDescription 
aliases: []

block_params: 
comment: 
- !ruby/struct:SM::Flow::P 
  body: Creates a form and a scope around a specific model object that is used as a base for questioning about values for the fields.
- !ruby/struct:SM::Flow::P 
  body: "Rails provides succinct resource-oriented form generation with <tt>form_for</tt> like this:"
- !ruby/struct:SM::Flow::VERB 
  body: "  &lt;% form_for @offer do |f| %&gt;\n    &lt;%= f.label :version, 'Version' %&gt;:\n    &lt;%= f.text_field :version %&gt;&lt;br /&gt;\n    &lt;%= f.label :author, 'Author' %&gt;:\n    &lt;%= f.text_field :author %&gt;&lt;br /&gt;\n  &lt;% end %&gt;\n"
- !ruby/struct:SM::Flow::P 
  body: There, <tt>form_for</tt> is able to generate the rest of RESTful form parameters based on introspection on the record, but to understand what it does we need to dig first into the alternative generic usage it is based upon.
- !ruby/struct:SM::Flow::H 
  level: 3
  text: Generic form_for
- !ruby/struct:SM::Flow::P 
  body: "The generic way to call <tt>form_for</tt> yields a form builder around a model:"
- !ruby/struct:SM::Flow::VERB 
  body: "  &lt;% form_for :person, :url =&gt; { :action =&gt; &quot;update&quot; } do |f| %&gt;\n    &lt;%= f.error_messages %&gt;\n    First name: &lt;%= f.text_field :first_name %&gt;&lt;br /&gt;\n    Last name : &lt;%= f.text_field :last_name %&gt;&lt;br /&gt;\n    Biography : &lt;%= f.text_area :biography %&gt;&lt;br /&gt;\n    Admin?    : &lt;%= f.check_box :admin %&gt;&lt;br /&gt;\n  &lt;% end %&gt;\n"
- !ruby/struct:SM::Flow::P 
  body: There, the first argument is a symbol or string with the name of the object the form is about, and also the name of the instance variable the object is stored in.
- !ruby/struct:SM::Flow::P 
  body: The form builder acts as a regular form helper that somehow carries the model. Thus, the idea is that
- !ruby/struct:SM::Flow::VERB 
  body: "  &lt;%= f.text_field :first_name %&gt;\n"
- !ruby/struct:SM::Flow::P 
  body: gets expanded to
- !ruby/struct:SM::Flow::VERB 
  body: "  &lt;%= text_field :person, :first_name %&gt;\n"
- !ruby/struct:SM::Flow::P 
  body: "If the instance variable is not <tt>@person</tt> you can pass the actual record as the second argument:"
- !ruby/struct:SM::Flow::VERB 
  body: "  &lt;% form_for :person, person, :url =&gt; { :action =&gt; &quot;update&quot; } do |f| %&gt;\n    ...\n  &lt;% end %&gt;\n"
- !ruby/struct:SM::Flow::P 
  body: In that case you can think
- !ruby/struct:SM::Flow::VERB 
  body: "  &lt;%= f.text_field :first_name %&gt;\n"
- !ruby/struct:SM::Flow::P 
  body: gets expanded to
- !ruby/struct:SM::Flow::VERB 
  body: "  &lt;%= text_field :person, :first_name, :object =&gt; person %&gt;\n"
- !ruby/struct:SM::Flow::P 
  body: "You can even display error messages of the wrapped model this way:"
- !ruby/struct:SM::Flow::VERB 
  body: "  &lt;%= f.error_messages %&gt;\n"
- !ruby/struct:SM::Flow::P 
  body: "In any of its variants, the rightmost argument to <tt>form_for</tt> is an optional hash of options:"
- !ruby/object:SM::Flow::LIST 
  contents: 
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>:url</tt> - The URL the form is submitted to. It takes the same fields you pass to <tt>url_for</tt> or <tt>link_to</tt>. In particular you may pass here a named route directly as well. Defaults to the current action.
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>:html</tt> - Optional HTML attributes for the form tag.
  type: :BULLET
- !ruby/struct:SM::Flow::P 
  body: Worth noting is that the <tt>form_for</tt> tag is called in a ERb evaluation block, not an ERb output block. So that's <tt>&lt;% %&gt;</tt>, not <tt>&lt;%= %&gt;</tt>.
- !ruby/struct:SM::Flow::P 
  body: "Also note that <tt>form_for</tt> doesn't create an exclusive scope. It's still possible to use both the stand-alone FormHelper methods and methods from FormTagHelper. For example:"
- !ruby/struct:SM::Flow::VERB 
  body: "  &lt;% form_for :person, @person, :url =&gt; { :action =&gt; &quot;update&quot; } do |f| %&gt;\n    First name: &lt;%= f.text_field :first_name %&gt;\n    Last name : &lt;%= f.text_field :last_name %&gt;\n    Biography : &lt;%= text_area :person, :biography %&gt;\n    Admin?    : &lt;%= check_box_tag &quot;person[admin]&quot;, @person.company.admin? %&gt;\n  &lt;% end %&gt;\n"
- !ruby/struct:SM::Flow::P 
  body: This also works for the methods in FormOptionHelper and DateHelper that are designed to work with an object as base, like FormOptionHelper#collection_select and DateHelper#datetime_select.
- !ruby/struct:SM::Flow::H 
  level: 3
  text: Resource-oriented style
- !ruby/struct:SM::Flow::P 
  body: As we said above, in addition to manually configuring the <tt>form_for</tt> call, you can rely on automated resource identification, which will use the conventions and named routes of that approach. This is the preferred way to use <tt>form_for</tt> nowadays.
- !ruby/struct:SM::Flow::P 
  body: For example, if <tt>@post</tt> is an existing record you want to edit
- !ruby/struct:SM::Flow::VERB 
  body: "  &lt;% form_for @post do |f| %&gt;\n    ...\n  &lt;% end %&gt;\n"
- !ruby/struct:SM::Flow::P 
  body: "is equivalent to something like:"
- !ruby/struct:SM::Flow::VERB 
  body: "  &lt;% form_for :post, @post, :url =&gt; post_path(@post), :html =&gt; { :method =&gt; :put, :class =&gt; &quot;edit_post&quot;, :id =&gt; &quot;edit_post_45&quot; } do |f| %&gt;\n    ...\n  &lt;% end %&gt;\n"
- !ruby/struct:SM::Flow::P 
  body: And for new records
- !ruby/struct:SM::Flow::VERB 
  body: "  &lt;% form_for(Post.new) do |f| %&gt;\n    ...\n  &lt;% end %&gt;\n"
- !ruby/struct:SM::Flow::P 
  body: expands to
- !ruby/struct:SM::Flow::VERB 
  body: "  &lt;% form_for :post, Post.new, :url =&gt; posts_path, :html =&gt; { :class =&gt; &quot;new_post&quot;, :id =&gt; &quot;new_post&quot; } do |f| %&gt;\n    ...\n  &lt;% end %&gt;\n"
- !ruby/struct:SM::Flow::P 
  body: "You can also overwrite the individual conventions, like this:"
- !ruby/struct:SM::Flow::VERB 
  body: "  &lt;% form_for(@post, :url =&gt; super_post_path(@post)) do |f| %&gt;\n    ...\n  &lt;% end %&gt;\n"
- !ruby/struct:SM::Flow::P 
  body: "And for namespaced routes, like <tt>admin_post_url</tt>:"
- !ruby/struct:SM::Flow::VERB 
  body: "  &lt;% form_for([:admin, @post]) do |f| %&gt;\n   ...\n  &lt;% end %&gt;\n"
- !ruby/struct:SM::Flow::H 
  level: 3
  text: Customized form builders
- !ruby/struct:SM::Flow::P 
  body: You can also build forms using a customized FormBuilder class. Subclass FormBuilder and override or define some more helpers, then use your custom builder. For example, let's say you made a helper to automatically add labels to form inputs.
- !ruby/struct:SM::Flow::VERB 
  body: "  &lt;% form_for :person, @person, :url =&gt; { :action =&gt; &quot;update&quot; }, :builder =&gt; LabellingFormBuilder do |f| %&gt;\n    &lt;%= f.text_field :first_name %&gt;\n    &lt;%= f.text_field :last_name %&gt;\n    &lt;%= text_area :person, :biography %&gt;\n    &lt;%= check_box_tag &quot;person[admin]&quot;, @person.company.admin? %&gt;\n  &lt;% end %&gt;\n"
- !ruby/struct:SM::Flow::P 
  body: "In this case, if you use this:"
- !ruby/struct:SM::Flow::VERB 
  body: "  &lt;%= render :partial =&gt; f %&gt;\n"
- !ruby/struct:SM::Flow::P 
  body: The rendered template is <tt>people/_labelling_form</tt> and the local variable referencing the form builder is called <tt>labelling_form</tt>.
- !ruby/struct:SM::Flow::P 
  body: "In many cases you will want to wrap the above in another helper, so you could do something like the following:"
- !ruby/struct:SM::Flow::VERB 
  body: "  def labelled_form_for(record_or_name_or_array, *args, &amp;proc)\n    options = args.extract_options!\n    form_for(record_or_name_or_array, *(args &lt;&lt; options.merge(:builder =&gt; LabellingFormBuilder)), &amp;proc)\n  end\n"
- !ruby/struct:SM::Flow::P 
  body: If you don't need to attach a form to a model instance, then check out FormTagHelper#form_tag.
full_name: ActionView::Helpers::FormHelper#form_for
is_singleton: false
name: form_for
params: (record_or_name_or_array, *args, &proc)
visibility: public
